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

2023-01-14Java开发问题
6

本文介绍了如何从 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 中的字符串中检测重复的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13