I am parsing file and I want to check each line against a few complicated regexs. Something like this
if re.match(regex1, line): do stuff
elif re.match(regex2, line): do other stuff
elif re.match(regex3, line): do still more stuff
...
Of course, to do the stuff, I need the match objects. I can only think of three possibilities, each of which leaves something to be desired.
if re.match(regex1, line):
m = re.match(regex1, line)
do stuff
elif re.match(regex2, line):
m = re.match(regex2, line)
do other stuff
...
which requires doing the complicated matching twice (these are long files and long regex :/)
m = re.match(regex1, line)
if m: do stuff
else:
m = re.match(regex2, line)
if m: do other stuff
else:
...
which gets terrible as I indent further and further.
while True:
m = re.match(regex1, line)
if m:
do stuff
break
m = re.match(regex2, line)
if m:
do other stuff
break
...
which just looks weird.
What's the right way to do this?