how to handle repetitive regexp match checks

Duncan Booth duncan.booth at invalid.invalid
Fri Mar 18 10:37:15 CET 2005


Matt Wette wrote:

> I am having difficulty doing this cleanly in python.  Can anyone help?
> 
>      rx1 = re.compile(r'struct {')
>      rx2 = re.compile(r'typedef struct {')
>      rx3 = re.compile(r'something else')
> 
>      m = rx1.match(line)
>      if m:
>        do something
>      else:
>        m = rx2.match(line)
>        if m:
>          do something
>        else:
>          m = rx3.match(line)
>          if m:
>        do something
>      else:
>        error
> 
> (In Scheme I was able to do this cleanly with macros.)

My preferred way to do this is something like this:

import re

RX = re.compile(r'''
   (?P<rx1> struct\s{ )|
   (?P<rx2> typedef\sstruct\s{  )|
   (?P<rx3> something\selse     )
''', re.VERBOSE)

class Matcher:
    def rx1(self, m):
        print "rx1 matched", m.group(0)

    def rx2(self, m):
        print "rx2 matched", m.group(0)

    def rx3(self, m):
        print "rx3 matched", m.group(0)

    def processLine(self, line):
        m = RX.match(line)
        if m:
            getattr(self, m.lastgroup)(m)
        else:
            print "error",repr(line),"did not match"

matcher = Matcher()
matcher.processLine('struct { something')
matcher.processLine('typedef struct { something')
matcher.processLine('something else')
matcher.processLine('will not match')




More information about the Python-list mailing list