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

2011-12-19 Thread Duncan Booth
MRAB pyt...@mrabarnett.plus.com wrote: On 16/12/2011 21:04, John Gordon wrote: Inmailman.3737.1324054637.27778.python-l...@python.org Devin Jeanpierrejeanpierr...@gmail.com writes: You could use re.finditer to find the longest match, and then replace it manually by hand (via string

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

2011-12-19 Thread ting
On Dec 16, 11:49 am, John Gordon gor...@panix.com wrote: I'm working with IPv6 CIDR strings, and I want to replace the longest match of (:|$)+ with :.  But when I use re.sub() it replaces the leftmost match, even if there is a longer match later in the string. Typically this means that

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

2011-12-19 Thread Ian Kelly
On Mon, Dec 19, 2011 at 4:15 PM, t...@thsu.org wrote: On Dec 16, 11:49 am, John Gordon gor...@panix.com wrote: I'm working with IPv6 CIDR strings, and I want to replace the longest match of (:|$)+ with :.  But when I use re.sub() it replaces the leftmost match, even if there is a

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

2011-12-16 Thread John Gordon
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

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

2011-12-16 Thread Devin Jeanpierre
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=lambda m: (m.end() = m.start())) -- Devin P.S. does anyone else get bothered by how

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

2011-12-16 Thread MRAB
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

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

2011-12-16 Thread Ian Kelly
On Fri, Dec 16, 2011 at 10:36 AM, MRAB pyt...@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

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

2011-12-16 Thread Ian Kelly
On Fri, Dec 16, 2011 at 10:57 AM, Ian Kelly ian.g.ke...@gmail.com wrote: On Fri, Dec 16, 2011 at 10:36 AM, MRAB pyt...@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

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

2011-12-16 Thread MRAB
On 16/12/2011 17:57, Ian Kelly wrote: On Fri, Dec 16, 2011 at 10:36 AM, MRABpyt...@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

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

2011-12-16 Thread Roy Smith
In article jcfsrk$skh$1...@reader1.panix.com, John Gordon gor...@panix.com wrote: I'm working with IPv6 CIDR strings, and I want to replace the longest match of (:|$)+ with :. But when I use re.sub() it replaces the leftmost match, even if there is a longer match later in the string.

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

2011-12-16 Thread John Gordon
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,

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

2011-12-16 Thread John Gordon
In mailman.3742.1324058429.27778.python-l...@python.org Ian Kelly ian.g.ke...@gmail.com writes: 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. pattern = r'\b0{1,3}([1-9a-f][0-9a-f]*|0)\b'

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

2011-12-16 Thread John Gordon
In roy-7c4e8a.13361716122...@news.panix.com Roy Smith r...@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

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

2011-12-16 Thread MRAB
On 16/12/2011 21:04, John Gordon wrote: Inmailman.3737.1324054637.27778.python-l...@python.org Devin Jeanpierrejeanpierr...@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

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

2011-12-16 Thread Terry Reedy
On 12/16/2011 1:36 PM, Roy Smith wrote: 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