如何匹配python中正则表达式中字符串列表中的任何字符串? [英] How to match any string from a list of strings in regular expressions in python?

查看:62
本文介绍了如何匹配python中正则表达式中字符串列表中的任何字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串列表,

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 = re.compile(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中正则表达式中字符串列表中的任何字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆