One more: re.compile("\\\\(\\d)")
. Or, a better option, a raw string: re.compile(r"\\(\d)")
.
The reason is the fact that backslash has meaning in both a string and in a regexp. For example, in regexp, \d
is "a digit"; so you can't just use \
for a backslash, and backslash is thus \\
. But in a normal string, \"
is a quote, so a backslash needs to be \\
. When you combine the two, the string "\\\\(\\d)"
actually contains \\(\d)
, which is a regexp that matches \
and a digit.
Raw strings avoid the problem up to a point by giving backslashes a different and much more restricted semantics.