如何测试字符串June
是否在给定列表中的任何正则表达式模式下测试True,同时也忽略大小写。我正在使用python来做到这一点。
['Ju*','October'] = True
['june','November'] = True
['*u*','October'] = True
['Junee','October'] = False
['October']` = False
['*'] = True
答案 0 :(得分:0)
您在这里:
def doesMatchPattern(self, string, patterns=[]):
for p in patterns:
# skip empty patterns
if not p: continue
# construct regex pattern
pattern = '^{}$'.format(p.lower())
if '*' in pattern:
pattern = pattern.replace('*', '[^\\s]*')
if re.match(pattern, string, re.I):
return True
return False
print doesMatchPattern('june', ['Ju*'])
print doesMatchPattern('june', ['june'])
print doesMatchPattern('june', ['*u*'])
print doesMatchPattern('june', ['Junee'])
print doesMatchPattern('june', ['October'])
print doesMatchPattern('june', ['*'])