Re: Doing both regex match and assignment within a If loop?

2013-03-29 Thread Steven D'Aprano
On Thu, 28 Mar 2013 21:00:44 -0700, Victor Hooi wrote: Is it possible to somehow test for a match, as well as do assignment of the re match object to a variable? mo = expression.match(line) if mo: ... Many problems become trivial when we stop trying to fit everything into a single

Re: Doing both regex match and assignment within a If loop?

2013-03-29 Thread Peter Otten
Victor Hooi wrote: Hi, I have logline that I need to test against multiple regexes. E.g.: import re expression1 = re.compile(r'') expression2 = re.compile(r'') with open('log.txt') as f: for line in f: if expression1.match(line):

Re: Doing both regex match and assignment within a If loop?

2013-03-29 Thread Alain Ketterlin
Victor Hooi victorh...@gmail.com writes: expression1 = re.compile(r'') expression2 = re.compile(r'') [...] Just a quick remark: regular expressions are pretty powerful at representing alternatives. You could just stick everything inside a single re, as in '...|...' Then use

Re: Doing both regex match and assignment within a If loop?

2013-03-29 Thread Arnaud Delobelle
On Friday, 29 March 2013, Alain Ketterlin wrote: Victor Hooi victorh...@gmail.com javascript:; writes: expression1 = re.compile(r'') expression2 = re.compile(r'') [...] Just a quick remark: regular expressions are pretty powerful at representing alternatives. You could

Re: Doing both regex match and assignment within a If loop?

2013-03-29 Thread Neil Cerutti
On 2013-03-29, Alain Ketterlin al...@dpt-info.u-strasbg.fr wrote: Victor Hooi victorh...@gmail.com writes: expression1 = re.compile(r'') expression2 = re.compile(r'') [...] Just a quick remark: regular expressions are pretty powerful at representing alternatives. You could

Re: Doing both regex match and assignment within a If loop?

2013-03-29 Thread Mitya Sirenef
On 03/29/2013 04:27 AM, Peter Otten wrote: (2) import re class Matcher: def __call__(self, expr, line): result = self.match = expr.match(line) return result def __getattr__(self, name): return getattr(self.match, name) Perhaps it's a little simpler to

Doing both regex match and assignment within a If loop?

2013-03-28 Thread Victor Hooi
Hi, I have logline that I need to test against multiple regexes. E.g.: import re expression1 = re.compile(r'') expression2 = re.compile(r'') with open('log.txt') as f: for line in f: if expression1.match(line): # Do something -

Re: Doing both regex match and assignment within a If loop?

2013-03-28 Thread Chris Rebert
On Thu, Mar 28, 2013 at 9:00 PM, Victor Hooi victorh...@gmail.com wrote: Hi, I have logline that I need to test against multiple regexes. E.g.: import re expression1 = re.compile(r'') expression2 = re.compile(r'') with open('log.txt') as f: for line in f: