使用 Python 匹配具有多个正则表达式的行 [英] Match a line with multiple regex using Python

查看:40
本文介绍了使用 Python 匹配具有多个正则表达式的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法查看一行是否包含与一组正则表达式匹配的单词?如果我有 [regex1, regex2, regex3],并且我想查看一行是否与其中任何一个匹配,我该怎么做?现在,我正在使用 re.findall(regex1, line),但它一次只匹配 1 个正则表达式.

Is there a way to see if a line contains words that matches a set of regex pattern? If I have [regex1, regex2, regex3], and I want to see if a line matches any of those, how would I do this? Right now, I am using re.findall(regex1, line), but it only matches 1 regex at a time.

推荐答案

您可以使用内置函数 any(或 all 如果所有正则表达式都必须匹配)和一个生成器表达式,用于遍历所有正则表达式对象.

You can use the built in functions any (or all if all regexes have to match) and a Generator expression to cicle through all the regex-objects.

any (regex.match(line) for regex in [regex1, regex2, regex3])

(or any(re.match(regex_str, line) for regex in [regex_str1, regex_str2, regex_str2]) 如果正则表达式不是预编译的正则表达式对象,当然)

(or any(re.match(regex_str, line) for regex in [regex_str1, regex_str2, regex_str2]) if the regexes are not pre-compiled regex objects, of course)

尽管与将您的正则表达式组合在单个表达式中相比,这会效率低下 - 如果此代码对时间或 CPU 至关重要,您应该尝试改为使用特殊的 | 组合一个包含您所有需求的单个正则表达式 正则表达式运算符来分隔原始表达式.组合所有正则表达式的一种简单方法是使用字符串join"运算符:

Although that will be ineficient compared to combining your regexes in a single expression - if this code is time or cpu critical, you should try instead, composing a single regular expression that encompass all your needs, using the special | regex operator to separate the original expressions. A simple way to combine all the regexs is to use the string "join" operator:

re.match("|".join([regex_str1, regex_str2, regex_str2]) , line)

尽管如果原始表达式已经使用了 | 运算符,则在此表单上组合正则表达式可能会导致错误的表达式.

Although combining the regexes on this form can result in wrong expressions if the original ones already do make use of the | operator.

这篇关于使用 Python 匹配具有多个正则表达式的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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