简体   繁体   中英

match string to list of regex patterns if statement

I am new to Python and trying to match a string to a list of regexes.

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):
    ...

Is this possible?

You can concatenate your expressions with the or operator | . You can then test your sentence/word on all of them together.

import re

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM