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

Posted by houbysoft on Stack Overflow See other posts from Stack Overflow or by houbysoft
Published on 2010-06-14T20:35:02Z Indexed on 2010/06/14 20:42 UTC
Read the original article Hit count: 279

Filed under:
|
|
|

Hi. I have a list of regexes in python, and a string. 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:.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about python

Related posts about regex