Raw strings and escaping

M

Matthew Warren

Hi,

I would expect this to work,

rawstring=r'some things\new things\some other things\'

But it fails as the last backslash escapes the single quote.

...although writing this I think I have solved my own problem. Is \' the
only thing escaped in a raw string so you can place ' in a raw string?
Although I thought the correct thing in that case would be;

rawstring=r"rawstring'with single-quote"


This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer.

You should not copy the email, use it for any purpose or disclose its contents to any other person.
Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
 
R

Rob Williscroft

Matthew Warren wrote in (e-mail address removed) in comp.lang.python:
I would expect this to work,

rawstring=r'some things\new things\some other things\'

It in the docs:

<url:http://docs.python.org/ref/strings.html#l2h-14>

.... Specifically, a raw string cannot end in a single backslash (since the
backslash would escape the following quote character). Note also that a
single backslash followed by a newline is interpreted as those two
characters as part of the string, not as a line continuation.

Rob.
 
D

Duncan Booth

Matthew Warren said:
I would expect this to work,

rawstring=r'some things\new things\some other things\'

But it fails as the last backslash escapes the single quote.

..although writing this I think I have solved my own problem. Is \'
the only thing escaped in a raw string so you can place ' in a raw
string? Although I thought the correct thing in that case would be;

rawstring=r"rawstring'with single-quote"

You cannot end *any* string literal with an odd number of backslash
characters. The "r" prefix changes how the escape sequences are interpreted
after the string literal has been parsed, it doesn't change how the literal
itself is actually parsed. See the Python Reference Manual, 2.4.1:
 
J

Jon Ribbens

I would expect this to work,

rawstring=r'some things\new things\some other things\'

But it fails as the last backslash escapes the single quote.

String constants in Python are weird - raw strings doubly so.
In a raw string, backslashes are not special - unless followed by
a quote character, when they simultaneously escape the quote and
also insert a literal backslash.

I presume there was originally some reason for this bizarre behaviour
- it'd be interesting to know what it is/was, if anyone knows?
 
D

Duncan Booth

Jon Ribbens said:
I presume there was originally some reason for this bizarre behaviour
- it'd be interesting to know what it is/was, if anyone knows?
See the FAQ for the explanation:

http://www.python.org/doc/faq/general/#why-can-t-raw-strings-r-strings-end-with-a-backslash

The idea is that if you use raw strings for regular expressions you can
write things like:

pattern = r'[\'"]'

if the raw string simply ignored the backslash altogether it would be much
harder to write regular expressions that contain both sorts of quotes.
 
J

Jon Ribbens

I presume there was originally some reason for this bizarre behaviour
- it'd be interesting to know what it is/was, if anyone knows?
See the FAQ for the explanation:

http://www.python.org/doc/faq/general/#why-can-t-raw-strings-r-strings-end-with-a-backslash

The idea is that if you use raw strings for regular expressions you can
write things like:

pattern = r'[\'"]'

if the raw string simply ignored the backslash altogether it would be much
harder to write regular expressions that contain both sorts of quotes.

Well, hardly *much* harder:

pattern = r"""foo"""

Personally, I think that raw strings behaving as they do is
unexpected, unintuitive and unnecessary, but it's obviously too late
to change it now anyway ;-)

I think standard strings accepting backslash followed by an unexpected
character is also a mistake, but again it's too late to fix now.
 
D

Duncan Booth

Jon Ribbens said:
Well, hardly *much* harder:

pattern = r"""foo"""

It means you have to always triple quote your raw strings or know in
advance of writing the regular expression which of r'', r"", r'''''',
r"""""" is most appropriate. The way it works at the moment you don't have
to care, you just write the raw string knowing that with r'' you have to
escape single quotes, r"" you have to escape double quotes and everything
works as expected.

Its only when you start trying to use raw strings for things other than
regular expressions that backslash at the end of the string can be a
problem.
 
S

Scott David Daniels

Matthew said:
Hi,

I would expect this to work,

rawstring=r'some things\new things\some other things\'

But it fails as the last backslash escapes the single quote.

Note something many people don't when looking over the string rules:

astring = r'some things\new things\some other things' '\\'

gives you exactly what you want, doesn't imply a string concatenation
at run time, and various other things. Two strings in succession are
concatenated at source translation time.

By the way, I renamed the result from being rawstring. It gives people
bad intuitions to refer to some strings as "raw" when what you really
mean is that the notation you are using is a "raw" notation for a
perfectly normal string.

--Scott David Daniels
(e-mail address removed)
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top