Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42218
| X-Received | by 10.224.157.1 with SMTP id z1mr1149866qaw.8.1364529644633; Thu, 28 Mar 2013 21:00:44 -0700 (PDT) |
|---|---|
| X-Received | by 10.50.170.68 with SMTP id ak4mr2354833igc.1.1364529644477; Thu, 28 Mar 2013 21:00:44 -0700 (PDT) |
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!ca1no15593848qab.0!news-out.google.com!v17ni9qad.0!nntp.google.com!ca1no15593843qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Thu, 28 Mar 2013 21:00:44 -0700 (PDT) |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=202.147.46.147; posting-account=LMx4fAoAAAAL3v616YFvkt1ueXc1H63- |
| NNTP-Posting-Host | 202.147.46.147 |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <f8598e49-67af-4a97-98db-9fd69d0182ae@googlegroups.com> (permalink) |
| Subject | Doing both regex match and assignment within a If loop? |
| From | Victor Hooi <victorhooi@gmail.com> |
| Injection-Date | Fri, 29 Mar 2013 04:00:44 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Xref | csiph.com comp.lang.python:42218 |
Show key headers only | View raw
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 - extract fields from line.
elif expression2.match(line):
# Do something else - extract fields from line.
else:
# Oh noes! Raise exception.
However, in the "Do something" section - I need access to the match object itself, so that I can strip out certain fields from the line.
Is it possible to somehow test for a match, as well as do assignment of the re match object to a variable?
if expression1.match(line) = results:
results.groupsdict()...
Obviously the above won't work - however, is there a Pythonic way to tackle this?
What I'm trying to avoid is this:
if expression1.match(line):
results = expression1.match(line)
which I assume would call the regex match against the line twice - and when I'm dealing with a huge amount of log lines, slow things down.
Cheers,
Victor
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Doing both regex match and assignment within a If loop? Victor Hooi <victorhooi@gmail.com> - 2013-03-28 21:00 -0700
Re: Doing both regex match and assignment within a If loop? Chris Rebert <clp2@rebertia.com> - 2013-03-28 21:29 -0700
Re: Doing both regex match and assignment within a If loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-29 06:45 +0000
Re: Doing both regex match and assignment within a If loop? Peter Otten <__peter__@web.de> - 2013-03-29 09:27 +0100
Re: Doing both regex match and assignment within a If loop? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2013-03-29 11:06 +0100
Re: Doing both regex match and assignment within a If loop? Arnaud Delobelle <arnodel@gmail.com> - 2013-03-29 10:41 +0000
Re: Doing both regex match and assignment within a If loop? Neil Cerutti <neilc@norwich.edu> - 2013-03-29 12:51 +0000
Re: Doing both regex match and assignment within a If loop? Mitya Sirenef <msirenef@lightbird.net> - 2013-03-29 09:49 -0400
csiph-web