Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex to replace double backslash with single backslash

Tags:

python

regex

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    
like image 534
mill Avatar asked May 21 '13 15:05

mill


1 Answers

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 '

like image 60
Inbar Rose Avatar answered Sep 30 '22 19:09

Inbar Rose