Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Fun with regular expressions solution in Clear category for Unix Match. Part 2 by carel.anthonissen
import re
def unix_match(filename: str, pattern: str) -> bool:
# Convert the pattern to a regular expression.
to_regex = {'.':'\.',
'*':'.+',
'?':'.',
'[!':'[^',
'[^]':'\[!\]'}
for key in to_regex:
pattern = pattern.replace(key, to_regex[key])
# Compile the regular expression and return False if it is invalid.
try:
regex = re.compile(pattern)
except re.error:
return False
# Check if the filename matched the pattern
return regex.fullmatch(filename) is not None
May 4, 2020
Comments: