Multiple regex match idiom

Charles Sanders C.delete_this.Sanders at BoM.GOv.AU
Wed May 9 05:37:21 EDT 2007


Hrvoje Niksic wrote:
> I often have the need to match multiple regexes against a single
> string, typically a line of input, like this:
> 
> if (matchobj = re1.match(line)):
>   ... re1 matched; do something with matchobj ...
> elif (matchobj = re2.match(line)):
>   ... re2 matched; do something with matchobj ...
> elif (matchobj = re3.match(line)):
> ....
[snip]
> 
> There are ways to work around the problem, for example by writing a
> utility predicate that passes the match object as a side effect, but
> that feels somewhat non-standard.  I'd like to know if there is a
> Python idiom that I'm missing.  What would be the Pythonic way to
> write the above code?

Only just learning Python, but to me this seems better.
Completely untested.

re_list = [ re1, re2, re3, ... ]
for re in re_list:
   matchob = re.match(line)
   if matchob:
     ....
     break

Of course this only works it the "do something" is the same
for all matches. If not, maybe a function for each case,
something like

re1 = re.compile(....)
def fn1( s, m ):
   ....
re2 = ....
def fn2( s, m ):
   ....

re_list = [ (re1, fn1), (re2, fn2), ... ]

for (r,f) in re_list:
   matchob = r.match(line)
   if matchob:
     f( line, matchob )
     break
     f(line,m)

Probably better ways than this exist.


Charles



More information about the Python-list mailing list