Regex in java for finding duplicate consecutive words

后端 未结 6 1866
时光说笑
时光说笑 2020-12-14 19:26

I saw this as an answer for finding repeated words in a string. But when I use it, it thinks This and is are the same and deletes the is

6条回答
  •  情歌与酒
    2020-12-14 19:51

    I believe this is the regular expression you should be using to detect 2 consecutive words separated by any number of non-word characters:

    Pattern p = Pattern.compile("\\b(\\w+)\\b\\W+\\b\\1\\b", Pattern.CASE_INSENSITIVE);
    

提交回复
热议问题