假设我有一个字符串列表,
Lets say I have a list of strings,
string_lst = ['fun', 'dum', 'sun', 'gum']我想创建一个正则表达式,在其中的某个点,我可以匹配我在该列表中的任何字符串,在一个组内,例如:
I want to make a regular expression, where at a point in it, I can match any of the strings i have in that list, within a group, such as this:
import re template = repile(r".*(elem for elem in string_lst).*") template.match("I love to have fun.")这样做的正确方法是什么?或者是否必须制作多个正则表达式并将它们分别与字符串匹配?
What would be the correct way to do this? Or would one have to make multiple regular expressions and match them all separately to the string?
推荐答案在管道符|上加入列表,代表正则表达式中的不同选项.
Join the list on the pipe character |, which represents different options in regex.
string_lst = ['fun', 'dum', 'sun', 'gum'] x="I love to have fun." print re.findall(r"(?=("+'|'.join(string_lst)+r"))", x)输出:['fun']
您不能使用 match,因为它会从一开始就匹配.使用 search 您将只获得第一个匹配项.所以改用 findall.
You cannot use match as it will match from start. Using search you will get only the first match. So use findall instead.
如果您的重叠匹配不是从同一点开始,也可以使用 lookahead.
Also use lookahead if you have overlapping matches not starting at the same point.
更多推荐
如何匹配python中正则表达式中字符串列表中的任何字符串?
发布评论