-1

I want to remove all special char such as '|', '.' or '$' in the string.

Here is my code:

string= '#$#&^&#$@||||123515'
re.sub(r'[^a-zA-Z0-9]', '', string)
print(string)

the output:

#$#&^&#$@||||123515

I know this regex means removing everything but number, a-z and A-Z.

But it fails to remove all special char.

Can somebody tell me why? Thank you! :)

Thomas Ayoub
  • 28,235
  • 15
  • 95
  • 135
Mars Lee
  • 1,645
  • 3
  • 16
  • 35

2 Answers2

4

This should help:

>>> import re
>>> string= '#$#&^&#$@||||123515'
>>> string = re.sub('[\W\_]','',string)
>>> string
'123515' 
Ahsanul Haque
  • 9,865
  • 4
  • 35
  • 51
1

Issue with your code: You have to store the response of re in string variable like string = re.sub('[^A-Za-z0-9]+', '', string). Then do print(string).

Alternative solution: Your can also achieve this even without using regex:

>>> string = '#$#&^&#$@||||123515'
>>> ''.join(e for e in string if e.isalnum())
123515
Moinuddin Quadri
  • 43,657
  • 11
  • 92
  • 117
  • This is [duplicate answer](http://stackoverflow.com/questions/1276764/stripping-everything-but-alphanumeric-chars-from-a-string-in-python). – Wiktor Stribiżew Mar 01 '16 at 09:27