[英]match string to list of regex patterns if statement
我是 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):
...
這可能嗎?
您可以使用 or 運算符連接您的表達式|
. 然后,您可以一起測試您的句子/單詞。
import re
r1 = r'lo'
r2 = r'hel.'
listofregex = [r1, r2]
regcombined = re.compile('|'.join(listofregex))
allmatched = regcombined.findall('hello')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.