I'm trying to replace all double backslashes with just a single backslash. I want to replace 'class=\\"highlight'
with 'class=\"highlight'
. I thought that python treats '\\'
as one backslash and r'\\+'
as a string with two backslashes. But when I try
In [5]: re.sub(r'\\+', '\\', string)
sre_constants.error: bogus escape (end of line)
So I tried switching the replace string with a raw string:
In [6]: re.sub(r'\\+', r'\\', string)
Out [6]: 'class=\\"highlight'
Which isn't what I need. So I tried only one backslash in the raw string:
In [7]: re.sub(r'\\+', r'\', string)
SyntaxError: EOL while scanning string literal
why not use string.replace()
?
>>> s = 'some \\\\ doubles'
>>> print s
some \\ doubles
>>> print s.replace('\\\\', '\\')
some \ doubles
Or with "raw" strings:
>>> s = r'some \\ doubles'
>>> print s
some \\ doubles
>>> print s.replace('\\\\', '\\')
some \ doubles
Since the escape character is complicated, you still need to escape it so it does not escape the '
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With