Backslash escape in regular expressions
peter at engcorp.com
Mon Dec 11 14:29:00 CET 2000
Jonathan Giddy wrote:
> According to the re module documentation, backslash either escapes special
> characters, or signals a special sequence. The special sequences are
> then listed.
> However, as this code shows, there are some special sequences (mainly
> the whitespace characters) that are special, but aren't listed. Is this
> a lapse in the re implementation or the re documentation? Can I safely
> expect re.compile(r'\(hello\)\n') to always match '(hello)\n' (the current
> behaviour) and not match '(hello)n' (the documented behaviour?)
The documentation I have clearly shows that \\ is a special sequence
which turns into the backslash character itself. With the raw-string
form with 'r' your "current behaviour" above *is* the documented
behaviour, isn't it? At least, just using those strings with "print"
shows that you don't get "(hello)n"...
More information about the Python-list