Re: Best way to extract from regex in if statement

2009-04-16 Thread Nick Craig-Wood
Paul McGuire wrote: > On Apr 3, 9:26 pm, Paul Rubin wrote: > > bwgoudey 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

Re: Best way to extract from regex in if statement

2009-04-04 Thread Paul McGuire
On Apr 3, 9:26 pm, Paul Rubin wrote: > bwgoudey 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(o

Re: Best way to extract from regex in if statement

2009-04-03 Thread Tim Chase
Or in case you want to handle each regexp differently, you can construct a dict {regexp : callback_function} that picks the right action depending on which regexp matched. One word of caution: dicts are unsorted, so if more than one regexp can match a given line, they either need to map to th

Re: Best way to extract from regex in if statement

2009-04-03 Thread Paul Rubin
bwgoudey 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):

Re: Best way to extract from regex in if statement

2009-04-03 Thread George Sakkis
On Apr 3, 9:56 pm, Jon Clements wrote: > On 4 Apr, 02:14, bwgoudey wrote: > > > > > I have a lot of if/elif cases based on regular expressions that I'm using to > > filter stdin and using print to stdout. Often I want to print something > > matched within the regular expression and the moment I'v

Re: Best way to extract from regex in if statement

2009-04-03 Thread Tim Chase
bwgoudey wrote: I have a lot of if/elif cases based on regular expressions that I'm using to filter stdin and using print to stdout. Often I want to print something matched within the regular expression and the moment I've got a lot of cases like: ... elif re.match("^DATASET:\s*(.+) ", line):

Re: Best way to extract from regex in if statement

2009-04-03 Thread Jon Clements
On 4 Apr, 02:14, bwgoudey wrote: > I have a lot of if/elif cases based on regular expressions that I'm using to > filter stdin and using print to stdout. Often I want to print something > matched within the regular expression and the moment I've got a lot of cases > like: > > ... > elif re.match("