?
Solved

Regular Expressions

Posted on 2006-06-07
4
Medium Priority
?
575 Views
Last Modified: 2010-04-16
Hi,

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

Andrew
0
Comment
Question by:REA_ANDREW
  • 2
4 Comments
 
LVL 7

Expert Comment

by:God_Ares
ID: 16851093
I usaly grab a book and try with expresso (google for it codeproject!)

http://regexlib.com/DisplayPatterns.aspx  <== ready made stuff... browse a bit

http://www.daveranck.com/SoftwareDev/NetReference/123.aspx
http://www.funduc.com/regexp.htm

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

Regards

Floris
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16851384
Something like this maybe:

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

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

Bob
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16851487
Bob,

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.

Thanks

Andrew :-)
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 16851637
sorry I forgot to include

using System.Text.RegularExpressions;

GRADE A.

Thank You AGain

Andrew
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 6 hours left to enroll

850 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