問題描述
可以通過哪些方式檢測字符串中的重復單詞?
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 中的字符串中檢測重復的單詞?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!