Regular Expressions

Posted on 2006-06-07
Last Modified: 2010-04-16

I need a regular Expression which will extract all the bad characters from a string so I am left with spaces, letters, numbers, underscores and dashes.

Can anyone point me in the direction of such an expression please

THanks in advance

Question by:REA_ANDREW
    LVL 7

    Expert Comment

    I usaly grab a book and try with expresso (google for it codeproject!)  <== ready made stuff... browse a bit

    these will help you (there is more out there ...) when seriously read you'll understand it soon enough!


    LVL 96

    Accepted Solution

    Something like this maybe:

     public static string StripCharacters(string input)
        string pattern = @"[!@#$%^&*()_+-=[\]{}|:;<>,.?/\\]+";

        return Regex.Replace(input, pattern, "");

    LVL 20

    Author Comment


    Thank you for that.  I have no doubt that this is correct, and that it is my application of the code that is wrong.

    public string RemoveBad(string TarStr)
                //Andy Says: Updated using Regular Expressions
                string pattern = @"[!@#$%^&*()_+-=[\]{}|:;<>,.?/\\]+";
                return Regex.Replace(TarStr, pattern, "");

    Please could you tell me what I am doing wrong here please as it does not remove any of the characters.


    Andrew :-)
    LVL 20

    Author Comment

    sorry I forgot to include

    using System.Text.RegularExpressions;

    GRADE A.

    Thank You AGain


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now