Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
bool(match) solution in Clear category for Unix Match. Part 2 by kalauroma7997
import re
PATTERNS = ('.', '\.'), ('?', '.'), ('*', '.*'), ('[!', '[^'), ('[^]', '\[!]'), ('[]', '[^.]')
def unix_match(filename, pattern):
for a, b in PATTERNS:
pattern = pattern.replace(a, b)
return bool(re.match(pattern, 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', 'somefile.txt') == True
assert unix_match('1name.txt', '[!abc]name.txt') == True
assert unix_match('log1.txt', 'log[!0].txt') == True
assert unix_match('log1.txt', 'log[1234567890].txt') == True
assert unix_match('log1.txt', 'log[!1].txt') == False
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 18, 2020