If need to say
if <this list has a string in it that matches this rexeg>:
do_stuff()
I found this powerful construct to extract matching strings from a list:
[m.group(1) for l in my_list for m in [my_regex.search(l)] if m]
...but this is hard to read and overkill. I don't want the list, I just want to know if such a list would have anything in it.
Is there a simpler-reading way to get that answer?
If you need to know if a string matches a regular expression RegExp , use RegExp.prototype.test() . If you only want the first match found, you might want to use RegExp.prototype.exec() instead.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
Method : Using join regex + loop + re.match() This task can be performed using combination of above functions. In this, we create a new regex string by joining all the regex list and then match the string against it to check for match using match() with any of the element of regex list.
You can simply use any
. Demo:
>>> lst = ['hello', '123', 'SO']
>>> any(re.search('\d', s) for s in lst)
True
>>> any(re.search('\d{4}', s) for s in lst)
False
use re.match
if you want to enforce matching from the start of the string.
Explanation:
any
will check if there is any truthy value in an iterable. In the first example, we pass the contents of the following list (in the form of a generator):
>>> [re.search('\d', s) for s in lst]
[None, <_sre.SRE_Match object at 0x7f15ef317d30>, None]
which has one match-object which is truthy, while None
will always evaluate to False
in a boolean context. This is why any
will return False
for the second example:
>>> [re.search('\d{4}', s) for s in lst]
[None, None, None]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With