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?
to combine two expressions or more, put every expression in brackets, and use: *? This are the signs to combine, in order of relevance: ?
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.
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.
$ means "Match the end of the string" (the position after the last character in the string).
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)
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