MRAB <pyt...@mrabarnett.plus.com> wrote: > On 16/12/2011 21:04, John Gordon wrote: >> In<mailman.3737.1324054637.27778.python-l...@python.org> Devin >> Jeanpierre<jeanpierr...@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 -- http://mail.python.org/mailman/listinfo/python-list