how to handle repetitive regexp match checks

David M. Cooke cookedm+news at physics.mcmaster.ca
Fri Mar 18 01:59:38 EST 2005


Matt Wette <matt.wette at earthlink.net> writes:

> Over the last few years I have converted from Perl and Scheme to
> Python.  There one task that I do often that is really slick in Perl
> but escapes me in Python.  I read in a text line from a file and check
> it against several regular expressions and do something once I find a match.
> For example, in perl ...
>
>      if ($line =~ /struct {/) {
>        do something
>      } elsif ($line =~ /typedef struct {/) {
>        do something else
>      } elsif ($line =~ /something else/) {
>      } ...
>
> 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

I usually define a class like this:

class Matcher:
    def __init__(self, text):
        self.m = None
        self.text = text
    def match(self, pat):
        self.m = pat.match(self.text)
        return self.m
    def __getitem__(self, name):
        return self.m.group(name)

Then, use it like

for line in fo:
    m = Matcher(line)
    if m.match(rx1):
        do something
    elif m.match(rx2):
        do something
    else:
        error

-- 
|>|\/|<
David M. Cooke
cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list