Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'string.': 0.05; '(all': 0.07; 'none,': 0.07; 'string': 0.09; "'a'": 0.09; 'matched': 0.09; 'subject:Why': 0.09; 'terminated': 0.09; 'def': 0.12; '[none,': 0.16; 'blocks': 0.16; 'combinations': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'grp': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'character': 0.29; 'flags': 0.31; 'piece': 0.31; 'run': 0.32; 'skip:_ 10': 0.34; 'received:84': 0.35; 'but': 0.35; 'subject:?': 0.36; 'should': 0.36; 'example,': 0.37; 'list': 0.37; 'ahead': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'skip:- 10': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'read': 0.60; 'completed': 0.61; 'first': 0.61; 'complete': 0.62; 'different': 0.65; 'yes': 0.68; 'containing': 0.69; 'florian': 0.84; 'regexp': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=E5NDpMtl c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=EysKIg0COvgA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=lXTksg8P43gkuhMgHOUA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Fri, 04 Jul 2014 16:57:19 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Why is regexp not working? References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404489628 news.xs4all.nl 2957 [2001:888:2000:d::a6]:56819 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73966 On 2014-07-04 13:27, Florian Lindner wrote: > Hello, > > I have that piece of code: > > def _split_block(self, block): > cre = [re.compile(r, flags = re.MULTILINE) for r in self.regexps] > block = "".join(block) > print(block) > print("-------------------") > for regexp in cre: > match = regexp.match(block) > for grp in regexp.groupindex: > data = match.group(grp) if match else None > self.data[grp].append(data) > > > block is a list of strings, terminated by \n. self.regexps: > > > self.regexps = [r"it (?P\d+) .* dt complete yes | > write-iteration-checkpoint |", > r"it (?P\d+) read ahead" > > > If I run my program it looks like that: > > > it 1 ahadf dt complete yes | write-iteration-checkpoint | > Timestep completed > > ------------------- > it 1 read ahead > it 2 ahgsaf dt complete yes | write-iteration-checkpoint | > Timestep completed > > ------------------- > it 4 read ahead > it 3 dfdsag dt complete yes | write-iteration-checkpoint | > Timestep completed > > ------------------- > it 9 read ahead > it 4 dsfdd dt complete yes | write-iteration-checkpoint | > Timestep completed > > ------------------- > it 16 read ahead > ------------------- > {'it_read_ahead': [None, '1', '4', '9', '16'], 'coupling_iterations': ['1', > None, None, None, None]} > > it_read_ahead is always matched when it should (all blocks but the first). > But why is the regexp containing coupling_iterations only matched in the > first block? > > I tried different combinations using re.match vs. re.search and with or > without re.MULTILINE. > The character '|' is a metacharacter that separates alternatives. For example, the regex 'a|b' will match 'a' or b'. Your regexes end with '|', which means that they will match an empty string at the start of the target string.