Suppose I want to change the blue dog and blue cat wore blue hats
to the gray dog and gray cat wore blue hats
.
With sed
I could accomplish this as follows:
$ echo 'the blue dog and blue cat wore blue hats' | sed 's/blue \(dog\|cat\)/gray \1/g'
How can I do a similar replacement in Python? I've tried:
>>> import re
>>> s = "the blue dog and blue cat wore blue hats"
>>> p = re.compile(r"blue (dog|cat)")
>>> p.sub('gray \1',s)
'the gray \x01 and gray \x01 wore blue hats'