RegEx用空格替换字符串中的特殊字符? asp.net C#
问题描述:
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";
答
对于下面的代码披露:
- 这不是测试
- 我可能搞砸字符转义
new Regex(...)
; -
我真的不知道C#,但我可以谷歌
"C# string replace regex"
和land on MSDNRegex re = new Regex("[;\\/:*?\"<>|&']"); string outputString = re.Replace(inputString, " ");
下面是正确的代码:
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"
答
您是否试图对SQL进行某种转义? – Qtax 2011-05-18 18:38:12