python raw strings?

M

Maurice LING

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
 
T

Terry Reedy

Maurice LING said:
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
 
C

Cy Edmunds

Maurice LING said:
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
 
L

Leif K-Brooks

Maurice said:
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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top