Regular Expression For Duplicate Words

后端 未结 13 1979
终归单人心
终归单人心 2020-11-22 11:13

I\'m a regular expression newbie, and I can\'t quite figure out how to write a single regular expression that would "match" any duplicate consecutive words such as

13条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 11:51

    This is the regex I use to remove duplicate phrases in my twitch bot:

    (\S+\s*)\1{2,}
    

    (\S+\s*) looks for any string of characters that isn't whitespace, followed whitespace.

    \1{2,} then looks for more than 2 instances of that phrase in the string to match. If there are 3 phrases that are identical, it matches.

提交回复
热议问题