栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java正则表达式匹配源码分析

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

Java正则表达式匹配源码分析

public String group(int group) {
    if (first < 0)
        throw new IllegalStateException("No match found");
    if (group < 0 || group > groupCount())
        throw new IndexOutOfBoundsException("No group " + group);
    if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
        return null;
    return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
}




public static void main(String[] args) {
    String content = "112233asdf55667788dsfsdf";
    Pattern pattern = Pattern.compile("(\d\d)(\d\d\d)(\d)");
    Matcher matcher = pattern.matcher(content);
    while (matcher.find()){
        System.out.println(matcher.group(0));
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        System.out.println(matcher.group(3));
    }

}

group(0)表示没有分组,group(1),group(2)...group(n)对字符串进行分组,groups[group*2]表示下一次出现的index。(这里的分组1,2,3对应上面的三组括号,如果增加4,则数组越界groups[-1]报错)

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

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

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