RegEx用空格替换字符串中的特殊字符?asp.net c#

Dot*_*per 9 c# regex asp.net

string inputString = "1/10 EP Sp'arrowha?wk XT R;TR 2.4GHz Red";
//Characters Collection: (';', '\', '/', ':', '*', '?', ' " ', '<', '>', '|', '&', ''')
string outputString = "1 10 EP Sp arrowha wk XT R TR 2.4GHz Red";
Run Code Online (Sandbox Code Playgroud)

Mat*_*all 21

关于以下代码的完整披露:

  • 它没有经过测试
  • 我可能搞砸了逃跑的角色new Regex(...);
  • 我实际上并不知道C#,但我可以谷歌"C# string replace regex"登陆MSDN

    Regex re = new Regex("[;\\/:*?\"<>|&']");
    string outputString = re.Replace(inputString, " ");
    
    Run Code Online (Sandbox Code Playgroud)

这是正确的代码:

string inputString = "1/10 EP Sp'arrowha?wk XT R;TR 2.4GHz R\\ed";
Regex re = new Regex("[;\\\\/:*?\"<>|&']");
string outputString = re.Replace(inputString, " ");
// outputString is "1 10 EP Sp arrowha wk XT R TR 2.4GHz R ed"
Run Code Online (Sandbox Code Playgroud)

演示:http://ideone.com/hrKdJ

另外:http://www.regular-expressions.info/