Best way to extract from regex in if statement

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

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

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):

More information about the Python-list mailing list