Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'mrab': 0.04; 'ok.': 0.04; 'string.': 0.04; ':-)': 0.06; 'ah,': 0.09; 'from:addr:python': 0.09; 'match.': 0.09; 'string)': 0.09; 'am,': 0.12; '16,': 0.15; 'cidr': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'ipv6': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'replaces': 0.16; 'reply-to:addr:python-list': 0.16; 'suggestions?': 0.16; 'zeroes': 0.16; 'this:': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'string,': 0.18; 'later': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; "i'm": 0.26; 'kelly': 0.30; 'pattern': 0.30; 'strings,': 0.30; 'subject:?': 0.31; 'matching': 0.32; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'there': 0.33; 'fri,': 0.34; 'match': 0.34; 'to:addr:python-list': 0.34; 'received:84': 0.34; 'reply-to:addr:python.org': 0.34; 'however,': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'replace': 0.38; 'group,': 0.40; 'to:addr:python.org': 0.40; 'according': 0.61; '2011': 0.61; 'john': 0.62; 'leading': 0.62; 'header:Reply-To:1': 0.71; 'reply- to:no real name:2**0': 0.72; 'match,': 0.73; '10:36': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=JaQ+XD2V c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=9jsOeB20M3cA:10 a=zL0Npd-XtY0A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=P4AJMxJaOCu29jqyOl0A:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 16 Dec 2011 18:19:52 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: re.sub(): replace longest match instead of leftmost match? References: <4EEB81B3.6020600@mrabarnett.plus.com> In-Reply-To: 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324059573 news.xs4all.nl 6879 [2001:888:2000:d::a6]:50029 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17371 On 16/12/2011 17:57, Ian Kelly wrote: > On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: >> On 16/12/2011 16:49, John Gordon wrote: >>> >>> According to the documentation on re.sub(), it replaces the leftmost >>> matching pattern. >>> >>> However, I want to replace the *longest* matching pattern, which is >>> not necessarily the leftmost match. Any suggestions? >>> >>> I'm working with IPv6 CIDR strings, and I want to replace the longest >>> match of "(0000:|0000$)+" with ":". But when I use re.sub() it replaces >>> the leftmost match, even if there is a longer match later in the string. >>> >>> I'm also looking for a regexp that will remove leading zeroes in each >>> four-digit group, but will leave a single zero if the group was all >>> zeroes. >>> >> How about this: >> >> result = re.sub(r"\b0+(\d)\b", r"\1", string) > > Close. > > pattern = r'\b0+([1-9a-f]+|0)\b' > re.sub(pattern, r'\1', string, flags=re.IGNORECASE) > Ah, OK. The OP said "digit" instead of "hex digit". That's my excuse. :-)