To include a literal backslash in a regular expression you must double
it, but to include one in a Python string literal you must also double
it. As a consequence, you have to QUADRUPLE a literal backslash
included in a regular expression written as a Python string literal.
Now you may ask, "but the \( and \) worked just fine". That is
because in Python string literals (and this is documented in the
reference manual!) a backslash followed by a character that isn't part
of one of the standard escape sequences (\n, \t, \ddd, etc. -- exactly
the same list as in Standard C) stays in the string. As far as I know
the only character that has a special meaning following a backslash in
regular expressions as well as in Python string literals is the
backslash itself.
Call it a minor nuisance. I'll add a note to the documentation of the
regex module.
--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"This is an ex-parrot"