473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 7078
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*********@acm.org> wrote in message
news:41********@news.unimelb.edu.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*********@acm.org> wrote in message
news:41********@news.unimelb.edu.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
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...
16
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...
10
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. ...
47
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.