473,799 Members | 2,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Raw strings and escaping

Hi,

I would expect this to work,

rawstring=r'som e 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"raw string'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 fromyour 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 ofDigica.
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
Oct 3 '06 #1
7 2087
Matthew Warren wrote in news:mailman.11 52.1159872720.1 0491.python-
li**@python.org in comp.lang.pytho n:
I would expect this to work,

rawstring=r'som e things\new things\some other things\'
It in the docs:

<url:http://docs.python.org/ref/strings.html#l2 h-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.
Oct 3 '06 #2
"Matthew Warren" <Ma************ @Digica.comwrot e:
I would expect this to work,

rawstring=r'som e 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"raw string'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:
When an "r" or "R" prefix is present, a character following a
backslash is included in the string without change, and all
backslashes are left in the string. For example, the string literal
r"\n" consists of two characters: a backslash and a lowercase "n".
String quotes can be escaped with a backslash, but the backslash
remains in the string; for example, r"\"" is a valid string literal
consisting of two characters: a backslash and a double quote; r"\" is
not a valid string literal (even a raw string cannot end in an odd
number of backslashes). 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.
Oct 3 '06 #3
In article <ma************ *************** ************@py thon.org>, Matthew Warren wrote:
I would expect this to work,

rawstring=r'som e 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?
Oct 3 '06 #4
Jon Ribbens <jo********@une quivocal.co.ukw rote:
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/genera...th-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.
Oct 3 '06 #5
In article <Xn************ *************@1 27.0.0.1>, Duncan Booth wrote:
>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/genera...th-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.
Oct 3 '06 #6
Jon Ribbens <jo********@une quivocal.co.ukw rote:
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.
Oct 3 '06 #7
Matthew Warren wrote:
Hi,

I would expect this to work,

rawstring=r'som e 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
sc***********@a cm.org
Oct 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4420
by: Dave Moore | last post by:
Hi All, Can anybody point me to a FAQ or similar that describes what all this stuff is about please?. I'm interfacing with a MySQL database if that's relavent. I've read a couple of books which refer to stripslahes and 'escaping' but nothing really explains what these terms are and why these are used. Why is 'escaping' (whatever that is) used?. What the hell is a magic quote?. How is it different from a non-magic one?. Regards, Dave
5
6348
by: bobbyballgame | last post by:
I am having a problem calling Stored Procedures: .... dim MyValue, MyOtherValue MyValue = "Bobby's value" MyOtherValue = Bobby's other value" rs.Open "exec MyStoredProc """ & MyValue & """, """ & MyOtherValue & """", Conn
2
3734
by: sorCrer | last post by:
Hi Guru's, Going crazy with this problem! I fill a dataset in asp.net with a 'FOR XML AUTO' query and Output it back to the browser. > DS.WriteXml(Response.OutputStream, XmlWriteMode.IgnoreSchema) No matter what I do I get the first nodes "<" encoded as "&lt;" This
5
12866
by: Micha³ Gancarski | last post by:
Hello! How do one unescape strings prepared with pg_escape_string() ? stripslashes() will not work because both these functions are not completely compatible. Thank you all in advance -- Micha³ Gancarski
5
4164
by: Alex | last post by:
Hi all - Is there a standard way to handle special chars in strings in dotnet? I'm using csharp and having two seperate problems... 1. Passing in args... I need to pass in an arg that points to a sqlserver2005 instance... it comes in looking like this in the debugger:
17
4448
by: john | last post by:
All: I'm a long-time developer, new to PHP.... Is there an idiom used in PHP to construct SQL statments from $_POST data? I would guess that in many applications, the data read from $_POST are used to build SQL statements. Certainly, we can do the following:
11
3672
by: krishnakant Mane | last post by:
hello, I finally got some code to push a pickled list into a database table. but now the problem is technically complex although possible to solve. the problem is that I can nicely pickle and store lists in a blob field with the help of dumps() for picklling into a string and then passing the string to the blob. I am also able to get back the string safely and do a loads() to unpickle the object. but this only works when list contains...
1
5485
by: David Henderson | last post by:
I know 'disable-output-escaping' has been discussed in the past, but I can't put my finger on any of the threads to see if my current problem is addressed. Sorry for re-asking the question if it has already been answered... I have an XML doc that I am transforming via XSLT and JavaScript in the browser. This allows me to return unsorted data to the browser and allow the user to sort it with a mouseclick and not hit the server just...
4
1037
by: regex_jedi | last post by:
ok, I have looked a lot of places, and can't seem to get a clear answer... I have a string called each_theme Some values of the string may contain a single quote as in - Happy Sad Nice
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.