Re: Parsing a string, removing any NON alphanumeric characters using regex

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


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?


Regex.Replace(username, @"[^\w-]+", "")


Relevant Pages

  • Re: Regex: Why is overreaching necessary?
    ... search target was stored in a variable). ... tail of the string unless they are explicitly ignored. ... characters in the middle to search against. ... regex should match ...
  • Re: Usename regex
    ... which have alphanumberic characters and one underscore. ... From what I understand is I can use a regex to do this. ... Think of a string, preferably very long that contains only alphanumeric characters, but end in a # sign. ...
  • Re: Regex: deleting non-matching words
    ... is to remove all words that do NOT strt with an underscore and simply ... the end of the string. ... regex lazily chooses 0 and completes a match. ... the end-of-line just before \n. ...
  • Re: algorithm to compare 3 array elements
    ... but the back reference doesn't. ... So if you make the regex global (adding ... return string; ... characters, and not skipping the first two. ...
  • Re: Regex techniques
    ... With any regex processor I have to use one of the "offical" regex ... VBS regex is supposed to treat end-of-line characters as valid characters ... when multi-line mode is set true. ... BUT it only seems to match the first, one-line, string. ...