Raw string substitution problem

E

Ed Keith

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-mail address removed)

Blog: edkeith.blogspot.com
 
C

Chris Hulan

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-mail address removed)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top