Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Unix Match solution in Clear category for Unix Match. Part 3 by colinmcnicholl
import re
def translate(pat):
"""Input: a UNIX shell PATTERN to a regular expression.
Function translates UNIX shell pattern to equivalent RE pattern.
Used as fnmatch module not allowed by Checkio.
Output: RE equivalent of UNIX shell pattern as a string.
"""
i, n = 0, len(pat)
res = ''
# loop over each character in pattern
while i < n:
c = pat[i]
i = i+1
if c == '*':
# '.*' is the RE translation for UNIX shell pattern '*'
# in re dot matches anything except newline and '*' specifies
# the previous character can be matched zero or more times.
res = res + '.*'
elif c == '?':
# '.' (dot) is the RE translation for UNIX shell pattern '?'
res = res + '.'
elif c == '[':
# If char in UNIX shell pattern is '[' note the index
j = i
# take one step forward provided not at end of pattern
# char immediately after '[' is '!'
if j < n and pat[j] == '!':
j = j+1
# take one step forward provided not at end of pattern
# char immediately after '[' is ']'
if j < n and pat[j] == ']':
j = j+1
# keep stepping forward as long as not at end or ']'
while j < n and pat[j] != ']':
j = j+1
if j >= n:
# if reached end of UNIX pattern and not seen ']'
res = res + '\\['
else:
# not reached end of UNIX pattern and seen ']'
# assign stuff to a copy of what was in the UNIX string with
# all occurrences of '\\' replaced with '\\\\'
# To match literal backslash write '\\\\' as RE string.
# Note: consider use of raw string
stuff = pat[i:j].replace('\\','\\\\')
i = j+1
if stuff[0] == '!':
# inside the character class the first character '^' is the
# RE translation of UNIX shell pattern '!'
stuff = '^' + stuff[1:]
elif stuff[0] == '^':
# If the first char after '[' in UNIX shell pattern is '^'
# use '\\' to match it and remove its special meaning.
stuff = '\\' + stuff
res = '%s[%s]' % (res, stuff)
else:
# escape the characters in the UNIX shell pattern except ASCII
# letters, numbers and '_' as UNIX shell pattern may have RE
# expression metacharacters within it.
res = res + re.escape(c)
return res + '\Z(?ms)'
def unix_match(filename: str, pattern: str) -> bool:
"""Input: filename as string and UNIX shell pattern as string.
Output: True if UNIX pattern matches filename, else False as BOOL."""
reg_expn = translate(pattern)
p = re.compile(reg_expn)
m = p.match(filename)
try:
if m.group() == filename:
return True
except:
return False
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!")
April 3, 2018
Comments: