When does the escape character work within raw strings?

Stephen Hansen apt.shansen at gmail.com
Sun May 24 00:36:06 CEST 2009


>
> > How do you know how a string object is going to be treated by any
> > given function?  Read the Fine Manual for that function.
>
> So am I to understand that there is no consistency in string handling
> throughout the standard modules/objects/methods?
>
> Seems to make python a lot more complicated than it needs to be, but
> okay.
>

What it seems like you're not understanding here is that regular expressions
are a special case.

\b means something in Python literals. It maps to a single character, \x08.
It _is_ a single character.

r"\b" is two characters, a slash followed by a b.

Regular expressions have an _entire_ range of their own special characters
that have nothing to do with Python special characters. For regular
expressions, \b -- that is, a slash followed by a b -- are TWO characters
(not one, like "\b" is to Python) that have special meaning.

Raw strings really exist SO you can do this. So you can do r"\b" and have
the slash-followed-by-b be encoded into two characters, which the re library
can then threat as a test for the blank string. Otherwise, you'd have to do
"\\b" and similar a LOT when building up regular expressions...

The re library just has its own rules for how it interprets strings. Its not
"no consistency". Its that regular expressions have meaning outside of
Python and Python's implementation follows that general meaning (some,
mostly, or to some other definition of how much).

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090523/9df75845/attachment.html>


More information about the Python-list mailing list