Regular expression to find and remove duplicate words

前端 未结 9 952
孤城傲影
孤城傲影 2020-11-30 09:46

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?

Ex.

9条回答
  •  孤街浪徒
    2020-11-30 09:56

    As said by others, you need more than a regex to keep track of words:

    var words = new HashSet();
    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);
    

提交回复
热议问题