0

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
  • 29,063
  • 15
  • 95
  • 142
Mars Lee
  • 1,845
  • 5
  • 17
  • 37

2 Answers2

5

This should help:

>>> import re
>>> string= '#$#&^&#$@||||123515'
>>> string = re.sub('[\W\_]','',string)
>>> string
'123515' 
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
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
  • 46,825
  • 13
  • 96
  • 126
  • 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