0

I would like to modify a string that looks like this:

...some fruit names... ORANGE ...some fruit names...

into something like this:

...some fruit names... ORANGE APPLE ...some fruit names...

With sed I would do something like:

sed 's/ORANGE/& APPLE/' <<< $the_string

However I don't know how to do this kind of thing with Python's re module. Is there a special sign which fonction is equivalent to the & of sed ? I haven't found anything like this in the documentation.

So far what I do is to extract the pattern ORANGE from the string (to get it's litteral form), append to it the string APPLE, and then substitute to ORANGE the string that I've formed. But it is very inefficient, and I would like to do the same thing with a single regexp.

4

1 回答 1

2

re.sub('(ORANGE)',r' \1 APPLE', str) will do it

于 2013-03-30T16:29:45.510 回答