Steven Bethard <[EMAIL PROTECTED]> wrote: > I get a bit uneasy from the repeated calls to m.group... 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): > self.mo = re.search(are, aline) > return self.mo > def groups(self, *indices): > return [self.mo.group(i) for i in indices] > > m = ReWithMemory() > > if m.search(r'add (\d+) (\d+)', line): > do_add(*m.groups(1, 2)) > elif m.search(r'mult (\d+) (\d+)', line): > do_mult(*m.groups(1, 2)) > elif m.search(r'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 m.group(1) 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. Alex -- http://mail.python.org/mailman/listinfo/python-list