Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; '"if': 0.04; 'character,': 0.07; 'though.': 0.07; 'python': 0.07; ':-)': 0.07; 'expressions.': 0.09; 'from:addr:python': 0.09; 'indicates': 0.09; 'subject:don': 0.09; '>>>': 0.12; 'wrote:': 0.14; 'captured': 0.16; 'capturing': 0.16; 'expression,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'matched': 0.16; 'matches.': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'parentheses': 0.16; 'pypi.': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'subject:regular': 0.16; 'appear': 0.19; 'header:In-Reply-To:1': 0.22; 'received:84': 0.25; 'times,': 0.25; 'says': 0.25; 'expect': 0.26; 'instead': 0.26; 'times.': 0.26; 'matches': 0.29; '"+"': 0.31; 'pattern': 0.31; 'fact': 0.31; 'match': 0.31; 'url:library': 0.31; 'to:addr:python-list': 0.32; 'url:docs': 0.33; 'expression': 0.33; 'module': 0.33; 'regular': 0.34; 'there': 0.35; 'that,': 0.35; 'header:User-Agent:1': 0.35; 'reply-to:addr:python.org': 0.35; 'surprised': 0.35; 'some': 0.37; 'way.': 0.37; 'should': 0.37; 'url:python': 0.37; 'but': 0.38; 'url:org': 0.38; 'returning': 0.39; 'to:addr:python.org': 0.39; 'would': 0.40; 'contained': 0.40; 'retrieve': 0.60; 'reply-to:no real name:2**0': 0.72; 'header:Reply-To:1': 0.72; 'subject:Groups': 0.84 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtYHACg7r01UXebj/2dsb2JhbACXd41Ld8cwhXEEkiE Date: Wed, 20 Apr 2011 21:03:48 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Groups in regular expressions don't repeat as expected References: <4daf31e3$0$10596$742ec2ed@news.sonic.net> In-Reply-To: <4daf31e3$0$10596$742ec2ed@news.sonic.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 27 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303329895 news.xs4all.nl 41102 [::ffff:82.94.164.166]:54877 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3746 On 20/04/2011 20:20, John Nagle wrote: > Here's something that surprised me about Python regular expressions. > > >>> krex = re.compile(r"^([a-z])+$") > >>> s = "abcdef" > >>> ms = krex.match(s) > >>> ms.groups() > ('f',) > > The parentheses indicate a capturing group within the > regular expression, and the "+" indicates that the > group can appear one or more times. The regular > expression matches that way. But instead of returning > a captured group for each character, it returns only the > last one. > > The documentation in fact says that, at > > http://docs.python.org/library/re.html > > "If a group is contained in a part of the pattern that matched multiple > times, the last match is returned." > > That's kind of lame, though. I'd expect that there would be some way > to retrieve all matches. > You should take a look at the regex module on PyPI. :-)