[Python-ideas] Proposed convenience functions for re module

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Jul 22 08:22:17 CEST 2009


MRAB wrote:

> Extend the current re.match and re.search to accept a tuple of patterns:
> 
>     m = re.match((pattern0, pattern1, pattern2, pattern3), s)
>     if m:
>         print m.group()

Also give the match object an 'index' attribute indicating
which pattern matched, so you can do

   m = re.match((pattern1, pattern2, pattern3, pattern4), s)
   if m:
     if m.index == 0:
       # pattern0 matched
     elif m.index == 1:
       # pattern1 matched
     # etc.

-- 
Greg




More information about the Python-ideas mailing list