Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
translate unix pattern to re pattern solution in Clear category for Unix Match. Part 1 by rossras
import re
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'\.')
.replace('*', '.*')
.replace('?', '.')
+ '$')
return bool(re.match(re_pattern, filename))
April 17, 2020
Comments: