如何从Java中的字符串中检测重复的单词?


问题内容

可以通过什么方式检测字符串中的重复单词?

例如,“这是重复测试的测试消息”包含一个重复单词测试。

在此,目标是检测出现在字符串中的所有重复单词。

使用正则表达式是实现目标的首选方法。


问题答案:

以下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 Test\n 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: is

Matching segment is "Test
 of duplicate test"
Duplicate word: Test

在这里,m.group(1)语句表示与第一组模式匹配的字符串[这里是(\\ w +)]。