Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:using': 0.04; 'patterns': 0.05; 'typed': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'wrote:': 0.15; "'type": 0.16; 'alternative,': 0.16; 'captured': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'returned,': 0.16; 'algorithm': 0.16; 'suggest': 0.17; 'appears': 0.21; 'maybe': 0.22; 'string': 0.26; 'tried': 0.27; 'character': 0.28; "didn't": 0.29; 'alternatives': 0.29; 'matches': 0.29; 'second': 0.29; 'match': 0.30; 'accidentally': 0.30; 'from:addr:web.de': 0.30; 'least': 0.31; 'seem': 0.31; 'to:addr :python-list': 0.34; 'header:X-Complaints-To:1': 0.34; 'position.': 0.34; 'example,': 0.35; 'starting': 0.35; 'sequence': 0.37; 'but': 0.37; 'using': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'skip:r 20': 0.40; 'your': 0.60; 'further': 0.65; 'alternative': 0.70; 'match,': 0.73; 'seeing,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: unexpected regexp behaviour using 'A|B|C.....' Date: Thu, 28 Jul 2011 12:57:18 +0200 Organization: None References: <6c92b791-58d2-4ea5-8997-48ef21ce69f8@z7g2000vbp.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084be0d.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311850649 news.xs4all.nl 23973 [2001:888:2000:d::a6]:56453 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10444 AlienBaby wrote: > When using re patterns of the form 'A|B|C|...' the docs seem to > suggest that once any of A,B,C.. match, it is captured and no further > patterns are tried. But I am seeing, > > st=' Id Name Prov Type CopyOf BsId > Rd -Detailed_State- Adm Snp Usr VSize' > > p='Type *' > re.search(p,st).group() > 'Type ' > > p='Type *| *Type' > re.search(p,st).group() > ' Type' > > > Shouldn’t the second search return the same as the first, if further > patterns are not tried? > > The documentation appears to suggest the first match should be > returned, or am I misunderstanding? All alternatives are tried at a given starting position in the string before the algorithm advances to the next position. The second alternative " *Type", at least one space followed by the character sequence "Type" matches right after "Prov" in your example, therefore the first alternative, "Type" and any following spaces, which would match after "Prov " is never tried. Maybe you accidentally typed one extra " "? If you didn't " +Type" would be clearer.