Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a list has one or more strings that match a regex

Tags:

python

regex

list

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?

like image 887
GreenAsJade Avatar asked Dec 29 '15 22:12

GreenAsJade


People also ask

How do you check if a regex matches a string?

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.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

What does regex 0 * 1 * 0 * 1 * Mean?

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.

How do you check if a string matches a regex pattern in Python?

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.


1 Answers

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]
like image 94
timgeb Avatar answered Nov 09 '22 06:11

timgeb