以下Java代码解决了从字符串中检测重复项的问题。如果重复的单词由换行符或标点符号分隔,则应该没有任何问题。
String duplicatePattern = "(?i)\b(\w+)\b[\w\W]*\b\1\b"; Pattern p = Pattern.compile(duplicatePattern); String phrase = "this is#$;%@;<>?|\` p is a is Testn of duplicate test"; Matcher m = p.matcher(phrase); String val = null; while (m.find()) { val = m.group(); System.out.println("Matching segment is "" + val + """); System.out.println("Duplicate word: " + m.group(1)+ "n"); }代码的输出将是:
Matching segment is "is#$;%@;<>?|` p is a is"Duplicate word: isMatching segment is "Test of duplicate test"Duplicate word: Test
在这里,m.group(1)语句表示与第一组模式匹配的字符串[这里是(\ w +)]。



