Doing both regex match and assignment within a If loop?

Peter Otten __peter__ at web.de
Fri Mar 29 09:27:46 CET 2013


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





More information about the Python-list mailing list