正则表达式查找并删除重复的单词 [英] Regular expression to find and remove duplicate words
本文介绍了正则表达式查找并删除重复的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中使用正则表达式时,有什么办法可以找到并删除含有多种文字和符号的字符串重复的文字或符号?
Using regular expressions in C#, is there any way to find and remove duplicate words or symbols in a string containing a variety of words and symbols?
例
字初始字符串:
我。像环境的环境还是不错的
"I like the environment. The environment is good."
所需的字符串:
我喜欢环境好
重复删除。的,环境,
推荐答案
正如所说别人,你需要比正则表达式更跟踪的话:
As said by others, you need more than a regex to keep track of words:
var words = new HashSet<string>();
string text = "I like the environment. The environment is good.";
text = Regex.Replace(text, "\\w+", m =>
words.Add(m.Value.ToUpperInvariant())
? m.Value
: String.Empty);
这篇关于正则表达式查找并删除重复的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文