How to detect duplicate words from a String in 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 中的字符串中检测重复的单词?
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01