Hello KH,
Regex is a bit overkill for that; you could...
string str = "AB&*^#Cabc(#&123--__";
StringBuilder sb = new StringBuilder(str.Length);
foreach (char ch in str)
{
if (Char.IsLetterOrDigit(ch)
|| ch == '-' || ch == '_')
{
sb.Append(ch);
}
}
str = sb.ToString();
Though, should your requirements become more complex, a regex solution like
the following can be used:
string cleaned = Regex.Replace("string to clean", "[^\w\d_-]", "", RegexOptions.None);
Just put all the characters you want to keep into the range above. Everything
else will be removed.
Jesse
>
"DotNetNewbie" wrote:
>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?
--
Jesse Houwing
jesse.houwing at sogeti.nl