Remove Non-Alphanumeric Characters from a String
Posted by mundeep on March 7, 2008
A colleague was looking for an easy way to remove all non-alphanumeric characters from a string and it took some time to find the easiest way was to use RegEx.Replace() as follows:
Regex.Replace(stringToCleanUp, "[\W]", "");
while \w (lowercase) matches any ‘word’ character, equivalent to [a-zA-Z0-9_]
\W matches any ‘non-word’ character, ie. anything NOT matched by \w
As an alternative if you don’t want to allow the underscore you can use [^a-zA-Z0-9].
The following regular expression quick reference helped in finding this solution:
Regular Expressions Quick Reference
risingsuns said
Thanks for the tip. It worked like a champ!
Allen said
Thanks for the info. Saved some trial and error.
cori said
Thanks for the tip – very helpful.
Small typo in the code, though. As the description states should be “[/W]” and not “[\W]“.
mundeep said
Sorry my description incorrectly stated /W, i have now corrected this (the code was already correct – see the examples on the two pages i have linked in the blog post).