Raw string substitution problem

Peter Otten __peter__ at web.de
Wed Dec 16 12:51:08 EST 2009


Ed Keith wrote:

> --- On Wed, 12/16/09, Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:
> 
>> From: Gabriel Genellina <gagsl-py2 at yahoo.com.ar>
>> Subject: Re: Raw string substitution problem
>> To: python-list at python.org
>> Date: Wednesday, December 16, 2009, 9:35 AM
>> En Wed, 16 Dec 2009 11:09:32 -0300,
>> Ed Keith <e_d_k at yahoo.com>
>> escribió:
>> 
>> > I am having a problem when substituting a raw string.
>> When I do the following:
>> > 
>> > re.sub('abc', r'a\nb\nc', '123abcdefg')
>> > 
>> > I get
>> > 
>> > """
>> > 123a
>> > b
>> > cdefg
>> > """
>> > 
>> > what I want is
>> > 
>> > r'123a\nb\ncdefg'
>> 
>> From http://docs.python.org/library/re.html#re.sub
>> 
>> re.sub(pattern, repl, string[, count])
>> 
>> ...repl can be a string or a function;
>> if
>> it is a string, any backslash escapes
>> in
>> it are processed. That is, \n is
>> converted
>> to a single newline character, \r is
>> converted to a linefeed, and so forth.
>> 
>> So you'll have to double your backslashes:
>> 
>> py> re.sub('abc', r'a\\nb\\nc', '123abcdefg')
>> '123a\\nb\\ncdefg'
>> 
>> --Gabriel Genellina
>> 
>> --http://mail.python.org/mailman/listinfo/python-list
>> 
> 
> That is going to be a nontrivial exercise. I have control over the
> pattern, but the texts to be substituted and substituted into will be read
> from user supplied files. I need to reproduce the exact text the is read
> from the file.

There is a helper function re.escape() that you can use to sanitize the  
substitution:

>>> print re.sub('abc', re.escape(r'a\nb\nc'), '123abcdefg')
123a\nb\ncdefg

Peter



More information about the Python-list mailing list