Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17367

Re: re.sub(): replace longest match instead of leftmost match?

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 <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'mrab': 0.04; 'string.': 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; 'this:': 0.16; 'wrote:': 0.18; 'string,': 0.18; 'cheers,': 0.20; 'later': 0.21; 'dec': 0.22; 'header:In- Reply-To:1': 0.22; 'received:74.125.82.174': 0.24; "i'm": 0.26; 'message-id:@mail.gmail.com': 0.28; 'pattern': 0.30; 'strings,': 0.30; 'subject:?': 0.31; '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; '\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=B/asCiaOonJK4PmcpzFJwpauaw498zjqib0AogpaOqw=; b=EvN7c+/036NXcXIfmI6uPkiNBq9ymMGO9besREmqd7VNeqWbV32/PklaLeQ3y8B7Cm wUKHb6PJ4DB+T1d2naZbAFwWrKwA/O5QAtV7WvHwR0Vg14Pq2wHlNp7fUoUhwTloMYev OkWCYlQ0isslmWoshYifbaHygXV0nogVl2DYU=
MIME-Version 1.0
In-Reply-To <4EEB81B3.6020600@mrabarnett.plus.com>
References <jcfsrk$skh$1@reader1.panix.com> <4EEB81B3.6020600@mrabarnett.plus.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 16 Dec 2011 10:57:06 -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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3740.1324058258.27778.python-list@python.org> (permalink)
Lines 29
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1324058259 news.xs4all.nl 6928 [2001:888:2000:d::a6]:56970
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17367

Show key headers only | View raw


On Fri, Dec 16, 2011 at 10:36 AM, MRAB <python@mrabarnett.plus.com> 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)

Cheers,
Ian

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

re.sub(): replace longest match instead of leftmost match? John Gordon <gordon@panix.com> - 2011-12-16 16:49 +0000
  Re: re.sub(): replace longest match instead of leftmost match? Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-16 11:56 -0500
    Re: re.sub(): replace longest match instead of leftmost match? John Gordon <gordon@panix.com> - 2011-12-16 21:04 +0000
      Re: re.sub(): replace longest match instead of leftmost match? MRAB <python@mrabarnett.plus.com> - 2011-12-16 21:36 +0000
        Re: re.sub(): replace longest match instead of leftmost match? Duncan Booth <duncan.booth@invalid.invalid> - 2011-12-19 15:46 +0000
  Re: re.sub(): replace longest match instead of leftmost match? MRAB <python@mrabarnett.plus.com> - 2011-12-16 17:36 +0000
  Re: re.sub(): replace longest match instead of leftmost match? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-16 10:57 -0700
  Re: re.sub(): replace longest match instead of leftmost match? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-16 10:59 -0700
    Re: re.sub(): replace longest match instead of leftmost match? John Gordon <gordon@panix.com> - 2011-12-16 21:06 +0000
  Re: re.sub(): replace longest match instead of leftmost match? MRAB <python@mrabarnett.plus.com> - 2011-12-16 18:19 +0000
  Re: re.sub(): replace longest match instead of leftmost match? Roy Smith <roy@panix.com> - 2011-12-16 13:36 -0500
    Re: re.sub(): replace longest match instead of leftmost match? John Gordon <gordon@panix.com> - 2011-12-16 21:07 +0000
    Re: re.sub(): replace longest match instead of leftmost match? Terry Reedy <tjreedy@udel.edu> - 2011-12-16 17:26 -0500
  Re: re.sub(): replace longest match instead of leftmost match? ting@thsu.org - 2011-12-19 15:15 -0800
    Re: re.sub(): replace longest match instead of leftmost match? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-19 19:58 -0700

csiph-web