Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
re.search() solution in Clear category for Unix Match. Part 3 by nickgryg
import re
def unix_match(filename: str, pattern: str) -> bool:
if bool(re.search('\[(.*)\]', pattern)) and pattern.count('[') > 1 and '[]' in pattern:
group_0 = re.search('\[(.*)\]', pattern).group(0)
regex = pattern.replace(f'[{group_0}]', group_0)
else:
regex = pattern.replace('!', '^').replace('[^]', '\[\!\]').replace('[]', 'NOTHING').replace('.', '\.').replace('?', '.').replace('*', '.*')
regex = f'^{regex}$'
return bool(re.search(regex, filename))
if __name__ == '__main__':
print("Example:")
print(unix_match('somefile.txt', '*'))
# These "asserts" are used for self-checking and not for an auto-testing
assert unix_match('somefile.txt', '*') == True
assert unix_match('other.exe', '*') == True
assert unix_match('my.exe', '*.txt') == False
assert unix_match('log1.txt', 'log?.txt') == True
assert unix_match('log1.txt', 'log[1234567890].txt') == True
assert unix_match('log12.txt', 'log?.txt') == False
assert unix_match('log12.txt', 'log??.txt') == True
print("Coding complete? Click 'Check' to earn cool rewards!")
Nov. 8, 2020