My dilemma: I'm passing my function a string that I need to then perform numerous regex manipulations on. The logic is if there's a match in the first regex, do one thing. If no match, check for a match with the second and do something else, if not check the third, and so forth. I could do something like this:
if re.match('regex1', string):
match = re.match('regex1', string)
# Manipulate match.group(n) and return
elif re.match('regex2', string):
match = re.match('regex2', string)
# Do second manipulation
[etc.]
However, this feels unnecessarily verbose, and usually when that's the case it means there's a better way that I'm either overlooking or don't yet know about.
Does anyone have a suggestion for a better way to do this (better from a code-appearance standpoint, a memory usage standpoint, or both)?
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.
Chaining regular expressions Regular expressions can be chained together using the pipe character (|). This allows for multiple search options to be acceptable in a single regex string.
Generally speaking, in these sorts of situations, you want to make the code "data driven". That is, put the important information in a container, and loop through it.
In your case, the important information is (string, function) pairs.
import re
def fun1():
print('fun1')
def fun2():
print('fun2')
def fun3():
print('fun3')
regex_handlers = [
(r'regex1', fun1),
(r'regex2', fun2),
(r'regex3', fun3)
]
def example(string):
for regex, fun in regex_handlers:
if re.match(regex, string):
fun() # call the function
break
example('regex2')
Similar question from back in september: How do you translate this regular-expression idiom from Perl into Python?
Using global variables in a module maybe not the best way to do it, but converting it into a class:
import re
class Re(object):
def __init__(self):
self.last_match = None
def match(self,pattern,text):
self.last_match = re.match(pattern,text)
return self.last_match
def search(self,pattern,text):
self.last_match = re.search(pattern,text)
return self.last_match
gre = Re()
if gre.match(r'foo',text):
# do something with gre.last_match
elif gre.match(r'bar',text):
# do something with gre.last_match
else:
# do something else
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