简体   繁体   English

Python:检查列表中是否至少有一个正则表达式匹配字符串的优雅方法

[英]Python: Elegant way to check if at least one regex in list matches a string

I have a list of regexes in python, and a string.我在 python 中有一个正则表达式列表和一个字符串。 Is there an elegant way to check if the at least one regex in the list matches the string?有没有一种优雅的方法来检查列表中的至少一个正则表达式是否与字符串匹配? By elegant, I mean something better than simply looping through all of the regexes and checking them against the string and stopping if a match is found.我所说的优雅,是指比简单地遍历所有正则表达式并根据字符串检查它们并在找到匹配项时停止更好的东西。

Basically, I had this code:基本上,我有这段代码:

list = ['something','another','thing','hello']
string = 'hi'
if string in list:
  pass # do something
else:
  pass # do something else

Now I would like to have some regular expressions in the list, rather than just strings, and I am wondering if there is an elegant solution to check for a match to replace if string in list: .现在我想在列表中有一些正则表达式,而不仅仅是字符串,我想知道是否有一个优雅的解决方案来检查匹配以替换if string in list:

import re

regexes = [
    # your regexes here
    re.compile('hi'),
#    re.compile(...),
#    re.compile(...),
#    re.compile(...),
]

mystring = 'hi'

if any(regex.match(mystring) for regex in regexes):
    print 'Some regex matched!'
import re

regexes = [
    "foo.*",
    "bar.*",
    "qu*x"
    ]

# Make a regex that matches if any of our regexes match.
combined = "(" + ")|(".join(regexes) + ")"

if re.match(combined, mystring):
    print "Some regex matched!"

A mix of both Ned's and Nosklo's answers. Ned和Nosklo的答案混合在一起。 Works guaranteed for any length of list... hope you enjoy 保证任何长度的列表...希望你喜欢

import re   
raw_lst = ["foo.*",
          "bar.*",
          "(Spam.{0,3}){1,3}"]

reg_lst = []
for raw_regex in raw_lst:
    reg_lst.append(re.compile(raw_regex))

mystring = "Spam, Spam, Spam!"
if any(compiled_reg.match(mystring) for compiled_reg in reg_lst):
    print("something matched")

Here's what I went for based on the other answers: 以下是我根据其他答案选择的内容:

raw_list = ["some_regex","some_regex","some_regex","some_regex"]
reg_list = map(re.compile, raw_list)

mystring = "some_string"

if any(regex.match(mystring) for regex in reg_list):
    print("matched")

If you loop over the strings, the time complexity would be O(n). 如果循环遍历字符串,则时间复杂度为O(n)。 A better approach would be combine those regexes as a regex-trie. 更好的方法是将这些正则表达式组合为正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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