473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Parsing a string, removing any NON alphanumeric characters usingregex

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?

Feb 26 '08 #1
3 20574
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
Feb 26 '08 #2
On Feb 26, 6:28 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
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
Replace doesn't take only 2 arguements, that doesn't work?
Feb 27 '08 #3
DotNetNewbie wrote:
On Feb 26, 6:28 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
>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-]+", "")

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
Feb 28 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
1
by: SStewart | last post by:
Does anyone know a good regex expression for removing all special characters from a string, allowing only alphanumeric?
3
by: Kiran A K | last post by:
hi, can anybody give me a c# method that removes non-alphanumeric characters from a string? i would prefer it if the code uses regular expressions.
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
2
by: dani kotlar | last post by:
Is there a function that copies between string (or stringbuilder) objects, while filtering out certain types or designated characters, or leaving a certain typa of designates\d characters? For...
15
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use...
4
by: cpptutor2000 | last post by:
Could some C guru provide some hints on my problem? I am trying to sort an array of character strings, where each string contains lowercase, uppercase, digits as well as non-alphanumeric characters...
6
by: James Arnold | last post by:
Hello, I am new to C and I am trying to write a few small applications to get some hands-on practise! I am trying to write a random string generator, based on a masked input. For example, given...
1
by: hd95 | last post by:
In a perfect world my xml feed source would produce perfect xml ..that is not the case I am parsing an XML feed that sometimes has ampersands and dashes in the content that messes up my parsing. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.