从字符串与正则表达式
问题描述:
删除标点我用正则表达式非常糟糕,但我想删除所有这些,;:!?“”$#@/* &^- +一个字符串的从字符串与正则表达式
string x = "This is a test string, with lots of: punctuations; in it?!.";
我怎样才能做到这
答
首先,请read here对正则表达式的信息,这是值得我们学习
您可以使用此:?。
Regex.Replace("This is a test string, with lots of: punctuations; in it?!.", @"[^\w\s]", "");
这意味着:
[ #Character block start.
^ #Not these characters (letters, numbers).
\w #Word characters.
\s #Space characters.
] #Character block end.
在读取结束“取代这不是一个单词字符或什么也没有空格字符的任意字符。”
为什么就不能运行与string.replace?性能毫无疑问会更好,代码将更易于启动。 – Tejs 2011-05-03 15:24:57
可能的重复[从字符串中去除标点符号的最佳方式](http://*.com/questions/421616/best-way-to-strip-punctuation-from-a-string) – 2011-05-03 15:26:45
@Tejs:性能可能或者可能不会更好,这取决于字符串的长度和需要替换的字符数。而且,代码不一定会不太可读。很多人不喜欢使用正则表达式,因为它们看起来很神秘,但就像任何其他代码一样 - 评论它们会对此有所帮助。 – 2011-05-03 15:27:29