Regular expression to find and remove duplicate words

前端 未结 9 942
孤城傲影
孤城傲影 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 10:00

    Well, Jeff has shown me how to use the magic of in-expression backreferences and the global modifier to make this one happen, so my original answer is inoperative. You should all go vote for Jeff's answer. However, for posterity I'll note that there's a tricky little regex engine sensitivity issue in this one, and if you were using Perl-flavored regex, you would need to do this:

    \b(\S+)\b(?=.*\b\1\b.*)
    

    instead of Jeff's answer, because C#'s regex will effectively capture \b in \1 but PCRE will not.

提交回复
热议问题