Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42220

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

References <f8598e49-67af-4a97-98db-9fd69d0182ae@googlegroups.com>
Date 2013-03-28 21:29 -0700
Subject Re: Doing both regex match and assignment within a If loop?
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.3934.1364531366.2939.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Mar 28, 2013 at 9:00 PM, Victor Hooi <victorhooi@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:
>             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()...

AFAIK, not without hacks and/or being unidiomatic.

> 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.

def process(line):
    match = expr1.match(line)
    if match:
        # ...extract fields…
        return something
    match = expr2.match(line)
    if match:
        # ...extract fields…
        return something
    # etc…
    raise SomeError()  # Oh noes!

with open('log.txt') as f:
    for line in f:
        results = process(line)


If you choose to further move the extractor snippets into their own
functions, then you can do:


# these could be lambdas if they're simple enough
def case1(match):
    # ...
def case2(match):
    # …
# etc...

REGEX_EXTRACTOR_PAIRS = [
    (re.compile(r'....'), case1),
    (re.compile(r'....'), case2),
    # etc...
]

def process(line):
    for regex, extractor in REGEX_EXTRACTOR_PAIRS:
        match = regex.match(line)
        if match:
            return extractor(match)
    raise SomeError()

Although this second option is likely somewhat less performant, but it
definitely saves on repetition.

Cheers,
Chris

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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