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

John Gordon gordon at panix.com
Fri Dec 16 16:07:30 EST 2011


In <roy-7C4E8A.13361716122011 at news.panix.com> Roy Smith <roy at panix.com> writes:

> Having done quite a bit of IPv6 work, my opinion here is that you're 
> trying to do The Wrong Thing.

> What you want is an IPv6 class which represents an address in some 
> canonical form.  It would have constructors which accept any of the 
> RFC-2373 defined formats.  It would also have string formatting methods 
> to convert the internal form into any of these formats.

> Then, instead of attempting to regex your way directly from one string 
> representation to another, you would do something like:

> addr_string = "FEDC:BA98:7654:3210:FEDC:BA98:7654:321"
> print IPv6(addr_string).to_short_form()

This does sound like a more robust solution.  I'll give it some thought.
Thanks Roy!

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list