I am confused with the backslash in regular expressions. Within a regex a
\ has a special meaning, e.g.
\d means a decimal digit. If you add a backslash in front of the backslash this special meaning gets lost. In the regex-howto one can read:
Perhaps the most important metacharacter is the backslash,
\. As in Python string literals, the backslash can be followed by various characters to signal various special sequences. It’s also used to escape all the metacharacters so you can still match them in patterns; for example, if you need to match a
\, you can precede them with a backslash to remove their special meaning:
print(re.search('\d', '\d')) gives
\d matches any decimal digit but there is none in
I now would expect
print(re.search('\\d', '\d')) to match
\d but the answer is still
print(re.search('\\\d', '\d')) gives as output
Does someone have an explanation?