Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!xlned.com!feeder3.xlned.com!amsnews11.chello.com!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'string.': 0.04; 'match.': 0.09; 'am,': 0.12; 'thanks!': 0.14; '16,': 0.15; 'edward': 0.15; '(via': 0.16; 'cidr': 0.16; 'ipv6': 0.16; 'replaces': 0.16; 'slicing).': 0.16; 'suggestions?': 0.16; 'zeroes': 0.16; 'cc:addr :python-list': 0.16; 'wrote:': 0.18; 'cc:no real name:2**0': 0.20; 'later': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'string': 0.24; 'cc:2**0': 0.24; '"the': 0.26; "i'm": 0.26; 'url:mailman': 0.28; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'received:209.85.210.46': 0.30; 'received:mail-pz0-f46.google.com': 0.30; 'strings,': 0.30; 'subject:?': 0.31; 'anyone': 0.31; 'does': 0.32; 'matching': 0.32; 'url:listinfo': 0.32; 'there': 0.33; 'fri,': 0.34; 'match': 0.34; 'largest': 0.35; 'url:python': 0.36; 'however,': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'replace': 0.38; 'received:209.85': 0.38; 'manually': 0.39; 'url:org': 0.39; 'else': 0.39; "it's": 0.40; 'group,': 0.40; 'received:209': 0.40; 'according': 0.61; '2011': 0.61; 'john': 0.62; 'leading': 0.62; 'p.s.': 0.67; 'bothered': 0.68; 'match,': 0.73; '11:49': 0.84; 'bears': 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 :cc:content-type:content-transfer-encoding; bh=La11bE3vNDVN6vkJsW1nOycX7ApoDTys9NH/Jg0pqc4=; b=YWzODYJfU41ANzHb2QoI686wx0mOObjNbtf0G895mrOQyi0d8OrsfkRTlnlYCzu0nN jF0Vu0/RVbeh2h0Xx89pySBE8Luj76BImOfcbtDpp2H982gURXgekB+aA7MLgVPEtvwy tTTdrHJ4CQsyqDyyFeIcGjGm6+leFV8dPDTgs= MIME-Version: 1.0 In-Reply-To: References: From: Devin Jeanpierre Date: Fri, 16 Dec 2011 11:56:32 -0500 Subject: Re: re.sub(): replace longest match instead of leftmost match? To: John Gordon Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324054637 news.xs4all.nl 6965 [2001:888:2000:d::a6]:47202 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17363 You could use re.finditer to find the longest match, and then replace it manually by hand (via string slicing). (a match is the longest if (m.end() - m.start()) is the largest -- so, max(re.finditer(...), key=3Dlambda m: (m.end() =3D m.start())) -- Devin P.S. does anyone else get bothered by how it's slice.start and slice.stop, but match.start() and match.end() ? On Fri, Dec 16, 2011 at 11:49 AM, 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. =C2=A0Any suggestions? > > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(0000:|0000$)+" with ":". =C2=A0But when I use re.sub() it repl= aces > 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. > > Thanks! > > -- > John Gordon =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 A is for Amy, who fell down the stairs > gordon@panix.com =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0B is for= Basil, assaulted by bears > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- Edward Gorey, "The Gashlycrumb Tin= ies" > > -- > http://mail.python.org/mailman/listinfo/python-list