Point Deep

Mundeep’s Tech Blog

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

4 Responses to “Remove Non-Alphanumeric Characters from a String”

  1. risingsuns said

    Thanks for the tip. It worked like a champ!

  2. Allen said

    Thanks for the info. Saved some trial and error.

  3. cori said

    Thanks for the tip – very helpful.

    Small typo in the code, though. As the description states should be “[/W]” and not “[\W]“.

  4. 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).

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>