Reputation: 22113
I learned the raw string in the re module.
In [16]: s
Out[16]: 'raw\\string'
In [17]: s = "raw\\string"
In [18]: s
Out[18]: 'raw\\string'
In [19]: re.sub(r"\", "", s)
File "<ipython-input-19-cebea97f2a9c>", line 1
re.sub(r"\", "", s)
^
SyntaxError: EOL while scanning string literal
Thus report error
In [23]: re.sub(r"\\", "", s)
Out[23]: 'rawstring'
r as raw here seems does nothing, since "\\" is escape \
and tried
re.sub("\\", "", s)
error: bad escape (end of pattern) at position 0
Is it a bug (in the perspective of a language for human) this might be solved with extra conditions in source code.
this is tricky than the arcane language elisp simply without the tricky concept of raw.
How could wrap my mind around this except remember many exceptions in learning python.
Upvotes: 0
Views: 79
Reputation: 24691
Just because it's a raw string in python does not mean it's interpreted literally in regex. We usually use raw strings for regex because if we don't, they go through python's processing and re
's processing (which means we need to double-escape every backslash we use). The pattern \
is not valid regex - it's just an escape character. The proper regex for the backslash character is \\
.
We can write this two ways in python: r'\\'
(raw string) or '\\\\'
(regular string which resolves to '\\'
).
Upvotes: 1