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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)