best way to do a series of regexp checks with groups

Alex Martelli aleaxit at
Mon Jan 24 04:28:25 EST 2005

Steven Bethard <steven.bethard at> wrote:

> I get a bit uneasy from the repeated calls to  If I was going
> to build a class around the re, I think I might lean towards something like:
> class ReWithMemory(object):
>      def search(self, are, aline):
> =, aline)
>          return
>      def groups(self, *indices):
>          return [ for i in indices]
> m = ReWithMemory()
> if'add (\d+) (\d+)', line):
>      do_add(*m.groups(1, 2))
> elif'mult (\d+) (\d+)', line):
>      do_mult(*m.groups(1, 2))
> elif'help (\w+)', line):
>      show_help(*m.groups(1))
> Of course, this is even less general-purpose than yours...

I'm not sure what advantage it's supposed to give.  Would you have any
problems writing, say, somecall(X[1], X[2]) ...?  Python normally relies
on indexing one thing at a time, and I see calling etc as
just the same kind of approach.

> (And if I saw myself using this much regex code, I'd probably reconsider
> my strategy anyway.) ;)

Surely joining all the regexp's into one big one with | would be faster
and more compact, but, with variable numbers of groups per sub-regexp,
determining which regexp matched can perhaps be tricky (issues with
matching mo.lastindex to the correct sub-regexp).  So, I can understand
the desire to do it sequentially, regexp by regexp.


More information about the Python-list mailing list