Alex Rivera | Logout

Remove all special characters, punctuation and spaces from string

Asked 2011-04-30T17:41:23.853
417

I need to remove all special characters, punctuation and spaces from a string so that I only have letters and numbers.

Edit
Report

1 Answer

410

Here is a regex to match a string of characters that are not a letters or numbers:

[^A-Za-z0-9]+

Here is the Python command to do a regex substitution:

re.sub('[^A-Za-z0-9]+', '', mystring)
answered 2011-04-30T17:46:17.710

Your Answer