Python regex matching in conditionals
You could define a function for the action required by each regex and do something like
def dostuff(): stuffdef dootherstuff(): otherstuffdef doevenmorestuff(): evenmorestuffactions = ((regex1, dostuff), (regex2, dootherstuff), (regex3, doevenmorestuff))for regex, action in actions: m = re.match(regex, line) if m: action() break
for patt in (regex1, regex2, regex3): match = patt.match(line) if match: if patt == regex1: # some handling elif patt == regex2: # more elif patt == regex3: # more break
I like Tim's answer because it separates out the per-regex matching code to keep things simple. For my answer, I wouldn't put more than a line or two of code for each match, and if you need more, call a separate method.
In this particular case there appears to be no convenient way to do this in python.if python would accept the syntax:
if (m = re.match(pattern,string)): text = m.group(1)
then all would be fine, but apparently youcannot do that