Best way to extract from regex in if statement

Paul Rubin http
Sat Apr 4 04:26:25 CEST 2009


bwgoudey <bwgoudey at gmail.com> writes:
> elif re.match("^DATASET:\s*(.+) ", line):
>         m=re.match("^DATASET:\s*(.+) ", line)
>         print m.group(1))

Sometimes I like to make a special class that saves the result:

  class Reg(object):   # illustrative code, not tested
     def match(self, pattern, line):
        self.result = re.match(pattern, line)
        return self.result

Then your example would look something like:

    save_re = Reg()
    ....
    elif save_re.match("^DATASET:\s*(.+) ", line):
       print save_re.result.group(1)



More information about the Python-list mailing list