Re: Regex in if statement.

2011-03-20 Thread Thomas L. Shinnick
At 07:46 PM 3/20/2011, Ken D'Ambrosio wrote: Hey, all -- I know how to match and return stuff from a regex, but I'd like to do an if, something like (from Perl, sorry): if (/MatchTextHere/){DoSomething();} How do I accomplish this in Python? You say you've done matching and accessing stuff fr

Re: Regex in if statement.

2011-03-20 Thread Rhodri James
On Mon, 21 Mar 2011 00:46:22 -, Ken D'Ambrosio wrote: Hey, all -- I know how to match and return stuff from a regex, but I'd like to do an if, something like (from Perl, sorry): if (/MatchTextHere/){DoSomething();} How do I accomplish this in Python? The basic routine is to do the match

Re: Regex in if statement.

2011-03-20 Thread python
I agree with previous comments. There are plenty of good tutorials, I have sometimes found more useful for learning than the manuals. The manuals are good but I prefer examples. http://www.tutorialspoint.com/python/python_reg_expressions.htm http://www.awaretek.com/tutorials.html#regular Bill Sn

Re: Regex in if statement.

2011-03-20 Thread Terry Reedy
On 3/20/2011 8:46 PM, Ken D'Ambrosio wrote: Hey, all -- I know how to match and return stuff from a regex, but I'd like to do an if, something like (from Perl, sorry): if (/MatchTextHere/){DoSomething();} How do I accomplish this in Python? Look at the doc to see what are the possible return

Re: Regex in if statement.

2011-03-20 Thread Benjamin Kaplan
On Sun, Mar 20, 2011 at 8:46 PM, Ken D'Ambrosio wrote: > Hey, all -- I know how to match and return stuff from a regex, but I'd > like to do an if, something like (from Perl, sorry): > > if (/MatchTextHere/){DoSomething();} > > How do I accomplish this in Python? > > Thanks! > > -Ken > Python doe

Regex in if statement.

2011-03-20 Thread Ken D'Ambrosio
Hey, all -- I know how to match and return stuff from a regex, but I'd like to do an if, something like (from Perl, sorry): if (/MatchTextHere/){DoSomething();} How do I accomplish this in Python? Thanks! -Ken -- http://mail.python.org/mailman/listinfo/python-list

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
of the duplication but I can't think of a nicer of way > > of doing this that will allow for a lot of these sorts of cases. Any > > suggestions? > > -- > > View this message in > > context:http://www.nabble.com/Best-way-to-extract-from-regex-in-i

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
of these sorts of cases. Any > suggestions? > -- > View this message in > context:http://www.nabble.com/Best-way-to-extract-from-regex-in-if-statement-... > Sent from the Python - python-list mailing list archive at Nabble.com. How about something like: your_regexes = [ re.co

Best way to extract from regex in if statement

2009-04-03 Thread bwgoudey
nabble.com/Best-way-to-extract-from-regex-in-if-statement-tp22878967p22878967.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list