Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42231
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'assignment': 0.07; 'lines,': 0.07; 'subject:both': 0.07; 'exception.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tackle': 0.09; 'def': 0.12; 'assume': 0.14; 'itself,': 0.16; 'match:': 0.16; 'name)': 0.16; 'name):': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'trying': 0.19; 'import': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'this:': 0.26; 'certain': 0.27; 'header:X-Complaints-To:1': 0.27; 'raise': 0.29; "i'm": 0.30; '"do': 0.31; 'extract': 0.31; 'strip': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'something': 0.35; '(2)': 0.35; 'test': 0.35; 'there': 0.35; 'possible': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'skip:n 10': 0.64; 'results': 0.69; 'victor': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Doing both regex match and assignment within a If loop? |
| Date | Fri, 29 Mar 2013 09:27:46 +0100 |
| Organization | None |
| References | <f8598e49-67af-4a97-98db-9fd69d0182ae@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p508492c7.dip.t-dialin.net |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3941.1364545653.2939.python-list@python.org> (permalink) |
| Lines | 73 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1364545653 news.xs4all.nl 6874 [2001:888:2000:d::a6]:52096 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:42231 |
Show key headers only | View raw
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):
> # 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.
(1)
for line in f:
match = expression1.match(line)
if match:
# ...
continue
match = expression2.match(line)
if match:
# ...
continue
raise NothingMatches
(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)
match = Matcher()
for line in f:
if match(expression1, line):
print(match.groupdict())
elif match(expression2, line):
print(match.group(1))
else:
raise NothingMatches
Back to comp.lang.python | Previous | Next — Previous in thread | 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