Raw string substitution problem

Chris Hulan chris.hulan at gmail.com
Wed Dec 16 09:36:35 EST 2009


On Dec 16, 9:09 am, Ed Keith <e_... at yahoo.com> wrote:
> 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'
>
> How do I get what I want?
>
> Thanks,
>
>     -EdK
>
> Ed Keith
> e_... at yahoo.com
>
> Blog: edkeith.blogspot.com

Looks like raw strings lets you avoid having to escape slashes when
specifying the literal, but doesn't preserve it during operations.
changing your replacement string to r'a\\nb\\nc' seems to give the
desired output

cheers



More information about the Python-list mailing list