检查字符串是否与python中的正则表达式模式列表匹配

时间:2018-09-21 15:33:12

标签: python

如何测试字符串June是否在给定列表中的任何正则表达式模式下测试True,同时也忽略大小写。我正在使用python来做到这一点。

['Ju*','October'] = True
['june','November'] = True
['*u*','October'] = True
['Junee','October'] = False
['October']` = False
['*'] = True

1 个答案:

答案 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', ['*'])