将字符串匹配到正则表达式模式列表(如果语句)

时间:2018-10-26 22:02:53

标签: python regex

我是Python的新手,正在尝试将字符串与正则表达式列表匹配。

import re
str = 'foo'
list1 = ['a', 'b', 'c']
listofRegex = [r'some*regex[.]pattern', r'some*regex[.]pattern2']

if str in list1 or re.match(rex in listofRegex, str):
    ...

这可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用or运算符|连接表达式。然后,您可以一起测试所有句子/单词。

import re

r1 = r'lo'
r2 = r'hel.'

listofregex = [r1, r2]
regcombined = re.compile('|'.join(listofregex))
allmatched = regcombined.findall('hello')