When does the escape character work within raw strings?

MRAB google at mrabarnett.plus.com
Wed May 20 14:05:10 EDT 2009


walterbyrd wrote:
> I know that
> 
> s = r'x\nx'
> 
> means 'x' followed by a literal '\' followed by an 'n' (the '\n' is
> not a carriage return).
> 
> s = r'x\tx'
> 
> means 'x' followed by a literal '\' followed by an 't' (the '\t' is
> not a tab).
> 
> But, boundries seem to work differently.
> 
> s = re.sub(r'\bxxx\b', 'yyy', s)
> 
> Is *not* going to look for a literal '\' followed by 'b'
> 
> So I am confused about when escapes work within raw strings, and when
> do escapes not work within raw strings.
 >
The re module receives the regular expression in the form of a string
and then interprets it in its own way.

If you give the re module '\b', that's a backspace character, which is
treated as a literal character, just like 'x'.

If you give it r'\b', that _does_ contain 2 characters, but the re
module interprets it as representing a word boundary, except in a
character set [...] where that would be meaningless, so there it's
interpreted as representing a backspace character.



More information about the Python-list mailing list