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

How to detect duplicate words from a String in Java?(如何从 Java 中的字符串中检测重复的单词?)

本文介绍了如何从 Java 中的字符串中检测重复的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What are the ways by which duplicate word in a String can be detected?

例如this is a test message for duplicate test"包含一个重复单词测试.

e.g. "this is a test message for duplicate test" contains one duplicate word test.

这里的目标是检测字符串中出现的所有重复单词.

Here, the objective is to detect all duplicate words which occur in a String.

最好使用正则表达式来实现目标.

Use of regular expression is preferable to achieve the goal.

推荐答案

以下 Java 代码解决了从字符串中检测重复项的问题.如果重复的单词用换行符或标点符号分隔应该没有任何问题.

The following Java code resolves the problem of detecting duplicates from a String. There should not be any problem if the duplicate word is separated by newline or punctuation symbols.

    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
 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)+ "
");
    }

代码的输出将是:

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+)].

Here, m.group(1) statement represents the String matched against 1st group of Pattern [here, it's (\w+)].

这篇关于如何从 Java 中的字符串中检测重复的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从 Java 中的字符串中检测重复的单词?

基础教程推荐