Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Unix Match. Part 1 by AlexG14
import re
def unix_match(filename: str, pattern: str) -> bool:
pattern = pattern.replace(".", "\.")
pattern = pattern.replace("*", ".*")
pattern = pattern.replace("?", ".")
return bool(re.match(pattern, filename))
print("Example:")
print(unix_match("somefile.txt", "*"))
# These "asserts" are used for self-checking
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("The mission is done! Click 'Check Solution' to earn rewards!")
Oct. 12, 2023
Comments: