Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'example:': 0.03; '(at': 0.04; "'',": 0.07; 'string': 0.09; "'')": 0.09; 'steps:': 0.09; 'cc:addr:python-list': 0.11; 'mean,': 0.16; 'positional': 0.16; 'to:addr:web.de': 0.16; 'url:html#re': 0.16; 'url:re': 0.16; 'wrote:': 0.18; 'example': 0.22; 'import': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'documented': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'matching': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; '>>>>': 0.31; 'interface': 0.32; 'url:python': 0.33; 'subject:the': 0.34; "can't": 0.35; 'tool': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'thank': 0.38; 'url:library': 0.38; 'url:mail': 0.40; 'how': 0.40; 'read': 0.60; 'more': 0.64; 'god': 0.65; 'subject:Get': 0.68; 'carefully': 0.74; "'first'": 0.84; 'otten': 0.84; 'positions:': 0.84; 'subject:groups': 0.84; 'crucial': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=fnew3wYZUhDrUlFEysZxpdfmy232rcV3ec9KG+Wuj4Y=; b=UWmgtdPYKeQqUhcj/VrqiZeMw2c9kQv5ijqGYjGt9mJmS6RxA693PFLdc1hvwi6vHO T+FPvpa9sDnOihY9a1hTpKLgnnYDbKAhFTqcFnLCnwchDlHBlP0E6LM/u625y5Cx5I4I gkZKgPOWZWZA5b/BwTAOkKgnsFV6iR7QBZf5uWjyJXLBrEuB5v2rp4izl9GgxSVSLMZV ayHj/eIAip1TmLoPm18uyCKX12uwP2WUUVYPJfwtlEDARZ9DNoNh4kaU02Lv7AD6J98E E0fpBoGH7W6nPVRk7+WND5S2QwchC8/KbHrU9K16mvqbzd0NErL3SpO0/VJZYnGqEaK9 YZjA== MIME-Version: 1.0 X-Received: by 10.107.137.218 with SMTP id t87mr40082002ioi.3.1428503826599; Wed, 08 Apr 2015 07:37:06 -0700 (PDT) In-Reply-To: References: Date: Wed, 8 Apr 2015 16:37:06 +0200 Subject: Re: Get the numbering of named regex groups From: Mattias Ugelvik To: Peter Otten <__peter__@web.de> Cc: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428503828 news.xs4all.nl 2840 [2001:888:2000:d::a6]:52379 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88669 Thank god it's that easy! Err, I mean, thank you! I should have read the docs more carefully :) On 08/04/2015, Peter Otten <__peter__@web.de> wrote: > Mattias Ugelvik wrote: > >> Example: re.match('(?Pa?)(?Pb?)', '') >> >> How can I find out that the group 'first' correlates to the positional >> regex group 1? I need to know this to resolve crucial ambiguities in a >> string manipulation tool I'm making. Looking at spans, as the example >> above illustrates, won't do the job. >> >> I can't see a way to do this through the documented interface (at >> least not in the `re` module?). > > Compile and match in two separate steps: > >>>> import re >>>> r = re.compile('(?Pa?)(?Pb?)') > > Find the groups' positions: > >>>> r.groupindex > {'second': 2, 'first': 1} > > > Find the matching substrings: > >>>> r.match("a").groupdict() > {'second': '', 'first': 'a'} > > https://docs.python.org/2.7/library/re.html#re.RegexObject.groupindex > > > -- > https://mail.python.org/mailman/listinfo/python-list >