Mark W
Mark W

Reputation: 85

How to replace all instances of "\/" with "/" in a string?

So I have a weird link like:

https\/\/blahblah.com\/path\/example

What I want to do is replace every "\/" with just a "/".

When I try

re.sub("\/", "/", soup)

It doesn't change anything. Any help is appreciated, thank you.

Upvotes: 0

Views: 4092

Answers (4)

Ram
Ram

Reputation: 130

mystring.replace("/", "\ /") its working for Me

Upvotes: -1

DamienzOnly
DamienzOnly

Reputation: 88

you can split by using split('\') method on the string and then join the list elements in a single string so that the backslashes are replaced

''.join(weird_string.split('\\')

Upvotes: 0

heemayl
heemayl

Reputation: 42007

Two things:

  • Make the Regex pattern raw string so Python does not do any per-interpretation and passes it to re as-is

  • Escape the \ with \; otherwise just \/ escapes / changing nothing as / is not anything needs escaping. So, you are replacing / with /.

So, use:

re.sub(r"\\/", "/", soup)

Example:

In [157]: s
Out[157]: 'https\\/\\/blahblah.com\\/path\\/example'

In [158]: re.sub(r"\\/", "/", s)
Out[158]: 'https//blahblah.com/path/example'

In [159]: s = 'https:\/\/dog.ceo\/api\/img\/labrador\/n02099712_7775.jpg'

In [160]: re.sub(r"\\/", "/", s)
Out[160]: 'https://dog.ceo/api/img/labrador/n02099712_7775.jpg'

Upvotes: 3

Xenon
Xenon

Reputation: 123

I cannot really tell you why re.sub does not work for your case, although I suspect some problem with the escape character interpretation of "/".

However,

s = 'https:\/\/blahblah.com\/path\/example'
s.replace('\/','/')

did what you want to do for me I think.

Upvotes: 0

Related Questions