栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

    String duplicatePattern = "(?i)\b(\w+)\b[\w\W]*\b\1\b";    Pattern p = Pattern.compile(duplicatePattern);    String phrase = "this is#$;%@;<>?|\` p is a is Testn 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)+ "n");    }

代码的输出将是:

Matching segment is "is#$;%@;<>?|` p is a is"Duplicate word: isMatching segment is "Test of duplicate test"Duplicate word: Test

在这里,m.group(1)语句表示与第一组模式匹配的字符串[这里是(\ w +)]。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号