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

Home Posts Topics Members FAQ

python raw strings?

I'm trying to toy around with PLY (python lex-yacc) by David Beazley
from University of Chicago and realized that the lex module uses python
raw strings. What are python raw strings and how are they different from
regular strings? I've tried to look up in the python documentation but
it just vaguely brushes through by saying that python raw strings are
prefixed with "r" or "R".

In PLY, "+" is "r'\+'" but "-" is "r'-'", why is there an extra "\" in
"+"? When to use the extra "\" and when not?

Can someone point me in the correct direction?

Thanks
Maurice
Jul 18 '05 #1
5 7104
On Tue, 24 Aug 2004 22:55:37 +0000, Maurice LING wrote:
I've tried to look up in the python documentation but
it just vaguely brushes through by saying that python raw strings are
prefixed with "r" or "R".

Can someone point me in the correct direction?


http://www.python.org/doc/2.3.4/ref/strings.html , last two paragraphs.
Jul 18 '05 #2

"Maurice LING" <ma*********@ac m.org> wrote in message
news:41******** @news.unimelb.e du.au...
I'm trying to toy around with PLY (python lex-yacc) by David Beazley
from University of Chicago and realized that the lex module uses python
raw strings. What are python raw strings


There is no such thing. Only string/unicode literals prefixed by 'r',
which prefix flags a change in the conversion of the literal into a Python
object. I believe the term come from 'raw' versus 'cooked' mode terminal
input in *nix.

Terry J. Reedy

Jul 18 '05 #3
"Maurice LING" <ma*********@ac m.org> wrote in message
news:41******** @news.unimelb.e du.au...
I'm trying to toy around with PLY (python lex-yacc) by David Beazley
from University of Chicago and realized that the lex module uses python
raw strings. What are python raw strings and how are they different from
regular strings? I've tried to look up in the python documentation but
it just vaguely brushes through by saying that python raw strings are
prefixed with "r" or "R".

In PLY, "+" is "r'\+'" but "-" is "r'-'", why is there an extra "\" in
"+"? When to use the extra "\" and when not?

Can someone point me in the correct direction?

Thanks
Maurice


http://docs.python.org/lib/module-re.html

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 18 '05 #4
Thanks. I find http://www.amk.ca/python/howto/regex/ to be invaluable in
understanding the concepts.

maurice

Maurice LING wrote:
I'm trying to toy around with PLY (python lex-yacc) by David Beazley
from University of Chicago and realized that the lex module uses python
raw strings. What are python raw strings and how are they different from
regular strings? I've tried to look up in the python documentation but
it just vaguely brushes through by saying that python raw strings are
prefixed with "r" or "R".

In PLY, "+" is "r'\+'" but "-" is "r'-'", why is there an extra "\" in
"+"? When to use the extra "\" and when not?

Can someone point me in the correct direction?

Thanks
Maurice

Jul 18 '05 #5
Maurice LING wrote:
What are python raw strings and how are they different from regular
strings?
First of all, a minor semantic correction. A string is not raw or
non-raw; a string can be expressed using raw syntax, but it's just a
different way of writing the same thing.

Now, in Python, strings can be written with special sequences involving
backslashes; for instance, "\n" means a line break instead of a
backslash and an n. If you want a literal backslash followed by an n,
you need to use two backslashes: "\\n".

If you need to use a lot of backslashes, doubling them up can get very
annoying. The raw string syntax fixes that: r"\n" is a backslash
followed by an n, not a line break.
In PLY, "+" is "r'\+'" but "-" is "r'-'", why is there an extra "\" in
"+"? When to use the extra "\" and when not?


I don't have any experience with PLY, but I'm guessing it uses regular
expressions (http://python.org/doc/current/lib/re-syntax.html). In a
regular expression, + means something special; if you don't want the
special meaning, you need to put a backslash in front of it.

The minus sign, on the other hand, has no special meaning in regular
expressions (well, outside of character sets), so it can be used on its
own with no backslash.
Jul 18 '05 #6

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

Similar topics

220
19179
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
16
2439
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold any arbitrary data, even binary data such as photos and movies.They are of course also good for their traditional role of storing and manipulating text." This view of strings is about a decade out of date with modern programmimg practice. From...
10
3696
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
47
3374
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols are objects whose representation within the code is more important than their actual value. Two symbols needs only to be
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
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.