正则表达式替换空间字符串特殊字符? asp.net C# [英] RegEx to replace special characters in a string with space ? asp.net c#
本文介绍了正则表达式替换空间字符串特殊字符? asp.net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
字符串inputString =1/10 EP Sp'arrowha周XT - [R; TR 2.4GHz的红;
//字符集:(';','\','/',':''?','*','','<','>',' |','&放大器;'','')
串outputString =1 10 EP SP arrowha周XT - [R TR 2.4GHz的红;
解决方案
对于下面的代码披露:
- 这不是测试
- 我可能搞砸字符
新正则表达式(...)
逃逸; -
我真的不知道C#,但我可以谷歌
C#字符串替换正则表达式
和land MSDN上正则表达式重新=新的正则表达式([; \\ /:* \<> |&放大器;']);
串outputString = re.Replace(inputString,);
下面是正确的代码:
字符串inputString =1/10 EP Sp'arrowha周XT - [R; TR 2.4GHz的红;
正则表达式重新=新的正则表达式([; \\\\ /:*?\<> |&放大器;']);
串outputString = re.Replace (inputString,);
// outputString是1 10 EP SP arrowha周XT - [R TR 2.4GHz的 - [R版
另外: http://www.regular-expressions.info/
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";
解决方案
Full disclosure regarding the following code:
- It's not tested
- I probably messed up the character escaping in
new Regex(...)
; I don't actually know C#, but I can Google for
"C# string replace regex"
and land on MSDNRegex re = new Regex("[;\\/:*?\"<>|&']"); string outputString = re.Replace(inputString, " ");
Here's the correct code:
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"
Demo: http://ideone.com/hrKdJ
Also: http://www.regular-expressions.info/
这篇关于正则表达式替换空间字符串特殊字符? asp.net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文