Fix Python – Regular Expressions: Search in list

Question

Asked By – leoluk

I want to filter strings in a list based on a regular expression.

Is there something better than [x for x in list if r.match(x)] ?

Now we will see solution for issue: Regular Expressions: Search in list


Answer

You can create an iterator in Python 3.x or a list in Python 2.x by using:

filter(r.match, list)

To convert the Python 3.x iterator to a list, simply cast it; list(filter(..)).

This question is answered By – sepp2k

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0