Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
re.match() solution in Clear category for Unix Match. Part 1 by kurosawa4434
from re import match, escape
def unix_match(filename, pattern):
# The solution is the subset of 'convert into regex'
# https://py.checkio.org/mission/unix-match/publications/kurosawa4434/python-3/convert-into-regex/
wildcards = {'*': '.*', '?': '.'}
escaping = lambda c: wildcards[c] if c in wildcards else escape(c)
regex = ''.join(map(escaping, pattern))
return match(regex, filename) is not None
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('log12.txt', 'log?.txt') == False
assert unix_match('log12.txt', 'log??.txt') == True
print("Coding complete? Click 'Check' to earn cool rewards!")
May 19, 2018
Comments: