Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine multiple regex into single one in python?

I'm learning about regular expression. I don't know how to combine different regular expression to make a single generic regular expression.

I want to write a single regular expression which works for multiple cases. I know this is can be done with naive approach by using or " | " operator.

I don't like this approach. Can anybody tell me better approach?

like image 397
Amit Avatar asked Feb 09 '17 11:02

Amit


People also ask

How do I combine multiple regex patterns?

to combine two expressions or more, put every expression in brackets, and use: *? This are the signs to combine, in order of relevance: ?

How do you do 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.

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.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


1 Answers

You need to compile all your regex functions. Check this example:

import re re1 = r'\d+\.\d*[L][-]\d*\s[A-Z]*[/]\d*' re2 = '\d*[/]\d*[A-Z]*\d*\s[A-Z]*\d*[A-Z]*' re3 = '[A-Z]*\d+[/]\d+[A-Z]\d+' re4 = '\d+[/]\d+[A-Z]*\d+\s\d+[A-Z]\s[A-Z]*'  sentences = [string1, string2, string3, string4] for sentence in sentences:     generic_re = re.compile("(%s|%s|%s|%s)" % (re1, re2, re3, re4)).findall(sentence) 
like image 50
Lior Magen Avatar answered Sep 16 '22 15:09

Lior Magen