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

如何检查整数重复序列

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

如何检查整数重复序列

您可以寻求正则表达式的帮助来解决此问题。考虑这样的代码:

String arr[] = {"12341234abc", "1234foo1234", "12121212", "111111111", "1a1212b123123c12341234d1234512345"};String regex = "(\d+?)\1";Pattern p = Pattern.compile(regex);for (String elem : arr) {    boolean noMatchFound = true;    Matcher matcher = p.matcher(elem);    while (matcher.find()) {        noMatchFound = false;        System.out.println(elem + " got repeated: " + matcher.group(1));    }    if (noMatchFound) {        System.out.println(elem + " has no repeation");    }}

输出:

abc12341234abc got repeated: 12341234foo1234 has no repeation12121212 got repeated: 1212121212 got repeated: 12111111111 got repeated: 1111111111 got repeated: 1111111111 got repeated: 1111111111 got repeated: 11a1212b123123c12341234d1234512345 got repeated: 121a1212b123123c12341234d1234512345 got repeated: 1231a1212b123123c12341234d1234512345 got repeated: 12341a1212b123123c12341234d1234512345 got repeated: 12345

说明:

使用正则表达式的

(\d+?)\1
地方

\d        - means a numerical digit\d+       - means 1 or more occurrences of a digit\d+?      - means reluctant (non-greedy) match of 1 OR more digits( and )    - to group the above regex into group # 1\1        - means back reference to group # 1(\d+?)\1 - repeat the group # 1 immediately after group # 1


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

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

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