Re: Parsing a string, removing any NON alphanumeric characters using regex



DotNetNewbie wrote:
I want to parse a string, ONLY allowing alphanumeric characters and
also the underscore '_' and dash '-' characters.

Anything else in the string should be removed.

I think my regex is looking like:

> ^([\w\d_-])*$

^[\w-]*$

should do - \w includes digits and underscore.

Now if I have this code:

string username = "mrcsharpis_so_cool!!!";

How can I strip all the characters that I dont' want?

Try:

Regex.Replace(username, @"[^\w-]+", "")

Arne
.