Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match a line with multiple regex using Python

Tags:

python

regex

Is there a way to see if a line contains words that matches a set of regex pattern? If I have [regex1, regex2, regex3], and I want to see if a line matches any of those, how would I do this? Right now, I am using re.findall(regex1, line), but it only matches 1 regex at a time.

like image 244
egidra Avatar asked Jan 17 '12 01:01

egidra


People also ask

How do you use multiple regex in Python?

made this to find all with multiple #regular #expressions. regex1 = r"your regex here" regex2 = r"your regex here" regex3 = r"your regex here" regexList = [regex1, regex1, regex3] for x in regexList: if re. findall(x, your string): some_list = re. findall(x, your string) for y in some_list: found_regex_list.

How do I search for multiple patterns in Python?

Search multiple words using regexUse | (pipe) operator to specify multiple patterns.

How do you match line breaks in regex?

If you want to indicate a line break when you construct your RegEx, use the sequence “\r\n”. Whether or not you will have line breaks in your expression depends on what you are trying to match. Line breaks can be useful “anchors” that define where some pattern occurs in relation to the beginning or end of a line.


2 Answers

You can use the built in functions any (or all if all regexes have to match) and a Generator expression to cycle through all the regex objects.

any (regex.match(line) for regex in [regex1, regex2, regex3])

(or any(re.match(regex_str, line) for regex in [regex_str1, regex_str2, regex_str2]) if the regexes are not pre-compiled regex objects, of course)

However, that will be inefficient compared to combining your regexes in a single expression. If this code is time- or CPU-critical, you should try instead to compose a single regular expression that encompasses all your needs, using the special | regex operator to separate the original expressions.

A simple way to combine all the regexes is to use the string join method:

re.match("|".join([regex_str1, regex_str2, regex_str2]), line)

A warning about combining the regexes in this way: It can result in wrong expressions if the original ones already do make use of the | operator.

like image 76
jsbueno Avatar answered Sep 26 '22 01:09

jsbueno


Try this new regex: (regex1)|(regex2)|(regex3). This will match a line with any of the 3 regexs in it.

like image 42
jok3rnaut Avatar answered Sep 22 '22 01:09

jok3rnaut