Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Parsing a string, removing any NON alphanumeric characters using     regex

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DotNetNewbie - 26 Feb 2008 23:20 GMT
Hi,

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_-])*$

Now if I have this code:

string username = "mrcsharpis_so_cool!!!";

How can I strip all the characters that I dont' want?
Arne Vajhøj - 26 Feb 2008 23:28 GMT
> I want to parse a string, ONLY allowing alphanumeric characters and
> also the underscore '_' and dash '-' characters.
[quoted text clipped - 4 lines]
>
> ^([\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
DotNetNewbie - 27 Feb 2008 15:39 GMT
> > I want to parse a string, ONLY allowing alphanumeric characters and
> > also the underscore '_' and dash '-' characters.
[quoted text clipped - 20 lines]
>
> Arne

Replace doesn't take only 2 arguements, that doesn't work?
Arne Vajhøj - 28 Feb 2008 01:23 GMT
>>> I want to parse a string, ONLY allowing alphanumeric characters and
>>> also the underscore '_' and dash '-' characters.
[quoted text clipped - 14 lines]
>
> Replace doesn't take only 2 arguements, that doesn't work?

There are 3 arguments in the call I suggest.

And Regex.Replace does have such a method:

http://msdn2.microsoft.com/en-us/library/e7f5w83z.aspx

Arne

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.