且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式查找并删除重复的单词

更新时间:2022-11-12 07:46:31

正如所说别人,你需要比正则表达式更跟踪的话:

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