[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: I can't understand re.sub
From:       Jussi Piitulainen <harvesting () is ! invalid>
Date:       2015-11-30 8:51:13
Message-ID: lf54mg3eupq.fsf () ling ! helsinki ! fi
[Download RAW message or body]

Erik writes:

> On 29/11/15 21:36, Mr Zaug wrote:
>> This should be simple, right?
>
> It is. And it could be even simpler if you don't bother with regexes
> at all (if your input is as fixed as you say it is):
>
> >>> foo = "foo bar baz spam CONTENT_PATH bar spam"
> >>> ' Substitute '.join(foo.split(' CONTENT_PATH ', 1))
> 'foo bar baz spam Substitute bar spam'

Surely the straight thing to say is:

   >>> foo.replace(' CONTENT_PATH ', ' Substitute ')
   'foo bar baz spam Substitute bar spam'

But there was no guarantee of spaces around the target. 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.
-- 
https://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic