簡體   English   中英

將字符串與正則表達式模式列表匹配 if 語句

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM