mill
mill

Reputation: 360

Python regex to replace double backslash with single backslash

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    

Upvotes: 13

Views: 40411

Answers (4)

Ruben
Ruben

Reputation: 187

Just use .replace() twice!

I had the following path: C:\\Users\\XXXXX\\Desktop\\PMI APP GIT\\pmi-app\\niton x5 test data

To convert \ to single backslashes, i just did the following:

path_to_file = path_to_file.replace('\\','*')
path_to_file = path_to_file.replace('**', '\\')

first operation creates ** for every \ and second operation escapes the first slash, replacing ** with a single \.

Result:

C:**Users**z0044wmy**Desktop**PMI APP GIT**pmi-app**GENERATED_REPORTS
C:\Users\z0044wmy\Desktop\PMI APP GIT\pmi-app\GENERATED_REPORTS

Upvotes: 2

BuJay
BuJay

Reputation: 127

I just realized that this might be the simplest answer:

import os
os.getcwd()

The above outputs a path with \ (2 back slashes)

BUT if you wrap it with a print function, i.e., print(os.getcwd()) it will output the 2 slashes with 1 slash so you can then copy and paste into an address bar!

Upvotes: 0

mogul
mogul

Reputation: 4553

You only got one backslash in string:

>>> string = 'class=\\"highlight' 
>>> print string
class=\"highlight

Now lets put another one in there

>>> string = 'class=\\\\"highlight' 
>>> print string
class=\\"highlight

and then remove it again

>>> print re.sub('\\\\\\\\', r'\\', string)
class=\"highlight

Upvotes: 2

Inbar Rose
Inbar Rose

Reputation: 43437

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 '

Upvotes: 17

Related Questions