题目:
[seq] matches any character in seq, for example [123] means any character - ‘1’, ‘2’ or ‘3’
[!seq] matches any character not in seq, for example [!123] means any character except ‘1’, ‘2’ and ‘3’
实例:
unix_match(‘somefile.txt’, ‘somefile.txt’) == True
unix_match(‘1name.txt’, ‘[!abc]name.txt’) == True
unix_match(‘log1.txt’, ‘log[1234567890].txt’) == True
链接:
https://py.checkio.org/en/mission/unix-match-part-2/
代码:
def unix_match(filename: str, pattern: str) -> bool:
a = filename
b = pattern
import re
rr = True
bb = re.findall('(\[.*?\])', b) #把b这些[*],作为一个整体一个一个取出来,放在例表里
mm = re.sub('(\[.*?\])','$',b) #b中去除bb的部分留下来
x= []
for i in mm:
if i != '$':
x.append(i)
qita = ''.join(x)
tiqu = []
if len(a) != len(mm):
rr = False
else:
for i in range(0, len(mm)):
if mm[i] == '$':
tiqu.append(a[i])
for i in range(0, len(tiqu)):
if qita[i] not in a:
rr = False
break
elif '!' in bb[i] and tiqu[i] in bb[i]:
rr = False
break
elif '!' not in bb[i] and tiqu[i] not in bb[i]:
rr = False
break
if bb == [] and b != a:
rr = False
if a == b:
rr = True
return rr