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

时间:2011-05-18 18:24:05

标签: c# asp.net regex

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";

3 个答案:

答案 0 :(得分:21)

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

  • 未经测试
  • 我可能搞砸了new Regex(...);
  • 中逃脱的角色
  • 我实际上并不知道C#,但我可以通过Google "C# string replace regex"land on MSDN

    Regex 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"

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

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

答案 1 :(得分:4)

string outputString = Regex.Replace(inputString,"[;\/:*?""<>|&']",String.Empty)

答案 2 :(得分:0)

这是替换特殊字符的Java代码

String inputString = "1/10 EP Sp'arrowha?wk XT R;TR 2.4GHz R\\ed";
String re = "[;\\\\/:*?\"<>|&']";
Pattern pattern = Pattern.compile(re);
Matcher matcher = pattern.matcher(inputString);
String outputString = matcher.replaceAll(" ");