re. help needed :(

Christophe Delord christophe.delord at free.fr
Fri Feb 22 15:17:27 EST 2002


Hi,

try this :


import re

x="Mary had a little lamb called lamb, but didn't like the lamb's mood"

print re.sub(r"lamb\b(?!')","pig",x)


\b is a word boundary and (?!...) matches if ... doesn't match what follows.



Christophe.


G. Willoughby wrote:

> Hi,
>     Im trying to replace the single word "lamb" and the word lamb followed
> by any punctuation in this string:
> 
> x="Mary had a little lamb called lamb, but didn't like the lamb's mood"
> 
> i want to replace all of the single words "lamb[punctuation]" but not the
> word "lamb's" to achieve this:
> 
> x="Mary had a little pig called pig, but didn't like the lamb's mood"
> 
> any help with be greatly appreiciated!
> 
> ta
> 
> G. Willoughby
> 
> 
> 


-- 
Christophe Delord
http://christophe.delord.free.fr/




More information about the Python-list mailing list