Home

C# Regex Remove Non-Alphanumeric Characters

Quick n dirty regex to remove non-alphanumeric characters

    public static string OnlyAlhpaNumeric(string FromThis)
    {
        return System.Text.RegularExpressions.Regex.Replace(FromThis, "[^a-zA-Z0-9]", string.Empty);
    }

What's going on? The regex expression checks for a-z, A-Z and 0-9, if it finds something that IS NOT in this list then it replaces it with string.Empty, ie nothing. Not a blank space or a null.

So with abcdefg%12345#ABCDEFG you would get back abcdef12345ABCDEFG

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog