efficient mega-replacements

Jonathan Hogg jonathan at onegoodidea.com
Fri Jan 25 08:00:35 EST 2002


On 25/1/2002 11:35, in article 83k7u6irxi.fsf at panacea.canonical.org, "Kragen
Sitaker" <kragen at pobox.com> wrote:

> Jonathan Hogg <jonathan at onegoodidea.com> writes:
>> In case you hadn't realised it I thought I'd note that using "regexp"
>> strings will save a lot of the stramash of backslashes in your code, as
>> follows:
>> 
>>             val = val.replace("'","''")\
>>                      .replace(r"\",r"\\\\\\\\")\
> 
> You didn't actually try this, did you?  :)

Yeah, yeah. I already got picked up on this :-)

My excuse is that I was in the middle of reinstalling Python as I was
writing the post...

>> The r'' form of string literals doesn't interpret backslashes.
> 
> Approximately true.

I thought approximate was enough. The edge cases are left as an exercise to
the reader ;-)

[Bah humbug! it was just meant to be a friendly suggestion to cut down on
the 16 backslashes in a row horror...]

Jonathan




More information about the Python-list mailing list