I can't understand re.sub

Erik python at lucidity.plus.com
Mon Nov 30 20:26:11 EST 2015


On 30/11/15 08:51, Jussi Piitulainen wrote:
> Surely the straight thing to say is:
>
>     >>> foo.replace(' CONTENT_PATH ', ' Substitute ')
>     'foo bar baz spam Substitute bar spam'

Not quite the same thing (but yes, with a third argument of 1, it would be).

> But there was no guarantee of spaces around the target.

I know. It was just an example to show that there might be an option 
that's not a regex for the specific use indicated. It's up to the OP to 
decide whether they think the spaces (or any other, or no, delimiter) 
would actually be required or useful. Or whether they really prefer a 
regex after all.

> If you wish to,
> say, replace "spam" in your foo with "REDACTED" but leave it intact in
> "May the spammer be prosecuted", a regex might be attractive after all.

But that's not what the OP said they wanted to do. They said everything 
was very fixed - they did not want a general purpose human language text 
processing solution ... ;)

E.



More information about the Python-list mailing list