Remove all slashes and backslashes

I have a line:

res = 'qwer!@ 234234 4234gdf36/\////// // \ \\\$%^$% dsfg'

and I want to remove ALL traits and backslashes. I tried this:

import string
import re

symbolsToRemove = string.punctuation
res = 'qwer!@ 234234 4234gdf36/\////// // \ \\\$%^$% dsfg'
res = re.sub(r'['+symbolsToRemove+']', ' ', res)
print(res)

But we get the following result:

qwer 234234 4234gdf36 \\\ dsfg

What am I doing wrong?

+4
source share
2 answers

This should work with re.escape:

>>> print re.sub(r'['+re.escape(symbolsToRemove)+']+', ' ', res)
qwer  234234 4234gdf36        dsfg
+1
source
import string
import re
symbolsToRemove = string.punctuation
res = 'qwer!@ 234234 4234gdf36/\////// // \ \\\$%^$% dsfg'
res = re.sub(r'[\\]*[\/]*','', res)
print(res)
+2
source

Source: https://habr.com/ru/post/1693687/


All Articles