Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17387
| Date | 2011-12-16 21:36 +0000 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: re.sub(): replace longest match instead of leftmost match? |
| References | <jcfsrk$skh$1@reader1.panix.com> <mailman.3737.1324054637.27778.python-list@python.org> <jcgbph$nnj$1@reader1.panix.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3752.1324071372.27778.python-list@python.org> (permalink) |
On 16/12/2011 21:04, John Gordon wrote:
> In<mailman.3737.1324054637.27778.python-list@python.org> Devin Jeanpierre<jeanpierreda@gmail.com> 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)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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