Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news.stack.nl!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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'mrab': 0.04; 'match.': 0.09; 'string)': 0.09; 'am,': 0.12; '16,': 0.15; 'cidr': 0.16; 'ipv6': 0.16; 'replaces': 0.16; 'suggestions?': 0.16; 'zeroes': 0.16; 'received:74.125.82.44': 0.16; 'received:mail- ww0-f44.google.com': 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; 'right.': 0.28; 'message- id:@mail.gmail.com': 0.28; 'kelly': 0.30; 'pattern': 0.30; 'strings,': 0.30; 'subject:?': 0.31; 'quite': 0.32; 'matching': 0.32; 'there': 0.33; 'fri,': 0.34; 'match': 0.34; 'to:addr:python- list': 0.34; 'received:74.125.82': 0.35; 'however,': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 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; 'match,': 0.73; '10:36': 0.84; '10:57': 0.84; '\xa0but': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=blrEHc1zbkPxKq92n8sCq6S4KK1LK9VkP2y0jAw2IBI=; b=JlwAH2Kim2/VGD+yTkaZgbYMtuRPj8moaK/71yCvSO+Go5gl91iokAnNMz8ZXRpxJZ UTDZJt6TIwkliIyPtzhaJ6q1WZXqX7qy99ubXoJxD2qvh3pveJNy6wMGKMueKiqIzDqK irD6y+ecTYujaY9Bzza0xsYUEnZOUThp0vOIs= MIME-Version: 1.0 In-Reply-To: References: <4EEB81B3.6020600@mrabarnett.plus.com> From: Ian Kelly Date: Fri, 16 Dec 2011 10:59:57 -0700 Subject: Re: re.sub(): replace longest match instead of leftmost match? To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 1324058429 news.xs4all.nl 6980 [2001:888:2000:d::a6]:60538 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17369 On Fri, Dec 16, 2011 at 10:57 AM, 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. =A0Any suggestions? >>> >>> I'm working with IPv6 CIDR strings, and I want to replace the longest >>> match of "(0000:|0000$)+" with ":". =A0But when I use re.sub() it repla= ces >>> 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 =3D re.sub(r"\b0+(\d)\b", r"\1", string) > > Close. > > pattern =3D r'\b0+([1-9a-f]+|0)\b' > re.sub(pattern, r'\1', string, flags=3Dre.IGNORECASE) Doh, that's still not quite right. pattern =3D r'\b0{1,3}([1-9a-f][0-9a-f]*|0)\b' re.sub(pattern, r'\1', string, flags=3Dre.IGNORECASE)