Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.039 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'from:addr:python': 0.09; '(via': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 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; 'reply-to:addr:python-list': 0.16; 'slicing).': 0.16; 'wrote:': 0.18; 'header:In-Reply-To:1': 0.22; 'string': 0.24; 'writes:': 0.25; 'colon': 0.30; 'subject:?': 0.31; 'header:User-Agent:1': 0.33; 'match': 0.34; 'to:addr:python-list': 0.34; 'probably': 0.34; 'received:84': 0.34; 'reply- to:addr:python.org': 0.34; 'largest': 0.35; 'something': 0.35; 'could': 0.37; 'doing': 0.38; 'using': 0.38; 'replace': 0.38; 'manually': 0.39; 'to:addr:python.org': 0.40; 'john': 0.62; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'match,': 0.73; '.replace': 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=8AHkEIZyAAAA:8 a=pGLkceISAAAA:8 a=zeCvV4WbZOltgQXMNwgA:9 a=wPNLvfGTeEIA:10 a=VhVvL8HfBcoA:10 a=MSl-tDqOz04A:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 16 Dec 2011 21:36:34 +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: 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324071372 news.xs4all.nl 6936 [2001:888:2000:d::a6]:41845 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17387 On 16/12/2011 21:04, John Gordon wrote: > In Devin Jeanpierre writes: > >> 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())) > > I ended up doing something similar: > > # find the longest match > longest_match = '' > for word in re.findall('((0000:?)+)', ip6): > if len(word[0])> len(longest_match): > longest_match = word[0] > > # if we found a match, replace it with a colon > if longest_match: > ip6 = re.sub(longest_match, ':', ip6, 1) > For a simple replace, using re is probably overkill. The .replace method is a better solution: ip6 = longest_match.replace(ip6, ':', 1)