Re: change only the nth occurrence of a pattern in a string

2009-01-14 Thread MRAB
Antoon Pardon wrote: > On 2008-12-31, TP wrote: >> Hi everybody, >> >> I would like to change only the nth occurence of a pattern in a string. The >> problem with "replace" method of strings, and "re.sub" is that we can only >> define the number of occurrences to change from the first one. >>

Re: change only the nth occurrence of a pattern in a string

2009-01-12 Thread Antoon Pardon
On 2008-12-31, TP wrote: > Hi everybody, > > I would like to change only the nth occurence of a pattern in a string. The > problem with "replace" method of strings, and "re.sub" is that we can only > define the number of occurrences to change from the first one. > v="coucou" v.replace("o

Re: change only the nth occurrence of a pattern in a string

2008-12-31 Thread Tim Chase
I would like to change only the nth occurence of a pattern in a string. The problem with "replace" method of strings, and "re.sub" is that we can only define the number of occurrences to change from the first one. v="coucou" v.replace("o","i",2) 'ciuciu' import re re.sub( "o", "i", v,2) 'c

Re: change only the nth occurrence of a pattern in a string

2008-12-31 Thread Steven D'Aprano
On Wed, 31 Dec 2008 15:40:32 +0100, TP wrote: > Hi everybody, > > I would like to change only the nth occurence of a pattern in a string. > The problem with "replace" method of strings, and "re.sub" is that we > can only define the number of occurrences to change from the first one. > v="co

Re: change only the nth occurrence of a pattern in a string

2008-12-31 Thread Roy Smith
In article <0scs26-7a5@rama.fbx.proxad.net>, TP wrote: > Hi everybody, > > I would like to change only the nth occurence of a pattern in a string. It's a little ugly, but the following looks like it works. The gist is to split the string on your pattern, then re-join the pieces using the

change only the nth occurrence of a pattern in a string

2008-12-31 Thread TP
Hi everybody, I would like to change only the nth occurence of a pattern in a string. The problem with "replace" method of strings, and "re.sub" is that we can only define the number of occurrences to change from the first one. >>> v="coucou" >>> v.replace("o","i",2) 'ciuciu' >>> import re >>> re