translate unix pattern to re pattern solution in Clear category for Unix Match. Part 1 by rossras
def unix_match(filename: str, pattern: str) -> bool:
# fnmatch does this, but we can't use it here, so use re instead.
# Some translation is needed:
# fn '.' = re r'\.': literal dot.
# fn '*' = re '.*': matches 0 or more of any character
# fn '?' = re '.' matches exactly 1 of any character
# Also, we add '$' to the end of the pattern to force the match to run to
# the end of the filename, instead of being a substring.
re_pattern = (pattern.replace('.', r'\.')
return bool(re.match(re_pattern, filename))
April 17, 2020