regex - Python: Why do raw strings require backslash to be escaped? -
this question has answer here:
- can't escape backslash regex? 5 answers
this explanation python documentation:
both string , bytes literals may optionally prefixed letter 'r' or 'r'; such strings called raw strings , treat backslashes literal characters. result, in string literals, '\u' , '\u' escapes in raw strings not treated specially. given python 2.x’s raw unicode literals behave differently python 3.x’s 'ur' syntax not supported.
if raw strings treat backslashes char literals, why backslash need escaped in expression:
re.compile(r"'\\'")
instead of being able write:
re.compile(r"'\'")
to capture single backslash when using re module?
because '\'
has special meaning in re means escape character after in language use define re if want match '+'
character re '\+'
Comments
Post a Comment