Use*_*987 6 c# regex asp.net special-characters c#-4.0
我想写一个正则表达式,将删除以下基础上的特殊字符:
@,&,',(,), <, >或#我写了这个正则表达式成功删除空格:
string username = Regex.Replace(_username, @"\s+", "");
Run Code Online (Sandbox Code Playgroud)
但我想升级/更改它,以便它可以删除我提到的上面的字符.
有人可以帮我解决这个问题吗?
Mit*_*pta 20
string username = Regex.Replace(_username, @"(\s+|@|&|'|\(|\)|<|>|#)", "");
Run Code Online (Sandbox Code Playgroud)
使用字符集 [charsgohere]
string removableChars = Regex.Escape(@"@&'()<>#");
string pattern = "[" + removableChars + "]";
string username = Regex.Replace(username, pattern, "");
Run Code Online (Sandbox Code Playgroud)