I am trying to extract a dataset from a string that can match one of three patterns. I have a list of compiled regular expressions. I want to run them (in order) and move on to the first match.
regexes = [
compiled_regex_1,
compiled_regex_2,
compiled_regex_3,
]
m = None
for reg in regexes:
m = reg.match(name)
if m: break
if not m:
print 'ARGL NOTHING MATCHES THIS!!!'
This should work (not tested yet), but it's pretty ugly. Is there a better way to boil a cycle that breaks when it succeeds or explodes when it doesn't?
There may be something special for rewhich I do not know about, which allows you to test several patterns as well.
source
share