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

Duncan Booth duncan.booth at invalid.invalid
Mon Dec 19 10:46:12 EST 2011


MRAB <python at mrabarnett.plus.com> wrote:

> On 16/12/2011 21:04, John Gordon wrote:
>> In<mailman.3737.1324054637.27778.python-list at python.org>  Devin
>> Jeanpierre<jeanpierreda at 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)

I think you got longest_match/ip6 backwards there.

Anyway, for those who like brevity:

try:
  ip6 = ip6.replace(max(re.findall('((?:0000:?)+)', ip6), key=len), ':', 1)
except ValueError: pass

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list