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: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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