I have
import re
cadena = re.sub('[^0-9a-zA-Z]+', '', cadena)
how to remove non-alphanumeric characters except this \ / @ + -:, | #
I have
import re
cadena = re.sub('[^0-9a-zA-Z]+', '', cadena)
how to remove non-alphanumeric characters except this \ / @ + -:, | #
Make sure to escape the backslash inside the character class. Use raw string for regular expression, it eliminates need in further escaping.
cadena = re.sub(r'[^0-9a-zA-Z\\/@+\-:,|#]+', '', cadena)
Please note that the underscore is considered an alphanumeric character. If you don't need to strip it, you can simplify the expression.
cadena = re.sub(r'[^\w\\/@+\-:,|#]+', '', cadena)
Example 1: (Most easy way I think)
# String
string = 'ABC#790@'
# Remove the #, @ and 7 from the string. (You can choose whatever you want to take out.)
line = re.sub('[#@7]', '', string)
# Print output
print(line)
Example 2:
You can also make use of substrings, that way you can look on every position if a letter matches the letter you don't want in your string. If the letter is one you would like to keep, you add that letter to a new string. Else you pass.
For example: String 's' is your string, and string 'd' conatain letters you want to filter out.
s = 'abc/geG#gDvs@h81'
d = '#@/'
e = ''
i = 0
j = 0
l = len(s)
g = len(d)
a = False
while i < l:
# Grab a single letter
letter1 = s[i:i+1]
# Add +1
i = i + 1
# Look if the letter is in string d
while j < g:
# Grab a single letter
letter2 = d[j:j+1]
# Add +1
j = j + 1
# Look if letter is not the same.
if (letter1 != letter2):
a = True
else:
a = False
break
# Reset j
j = 0
# Look if a is True of False.
# True: Letter does not contain letters from string d.
# False: Letter does contain letters from string d.
if (a == True):
e = e + letter1
else:
pass
# Print the result
print(e)