group是针对()来说的,group(0)指的整个串,group(1) 指的是第一个括号里的东西,group(2)指第二个括号里的东西。
@Test public void test01() { String regex = "(你*.?啊)|(AAA.*?BBB)|(CCC.*?DDD)"; Pattern pattern = Pattern.compile(regex); String input = "你好啊|AAA123456内容2BcBB|CCC内容3DDD|CCC内容3DDD"; Matcher matcher = pattern.matcher(input); log.info("匹配子模式数量: {}",matcher.groupCount()); // find() 像迭代器那样向前遍历输入字符串 while (matcher.find()) { // log.info("匹配项:{}",matcher.pattern().toString()); /*group(0)就是指的整个串,group(1) 指的是第一个括号里的东西,group(2)指的第二个括*/ log.info("??????????????????????????????????????????????"); log.info("grou0 整串-> {},start -> {} ,end -> {}",matcher.group(0),matcher.start(0),matcher.end(0)); log.info("grou1 -> {},start -> {} ,end -> {}",matcher.group(1),matcher.start(1),matcher.end(1)); log.info("grou2 -> {},start -> {} ,end -> {}",matcher.group(2),matcher.start(2),matcher.end(2)); log.info("grou3 -> {},start -> {} ,end -> {}",matcher.group(3),matcher.start(3),matcher.end(3)); } }
输出结果:
[21:39:24:629] [INFO] - (test001.java:19) - 匹配子模式数量: 3 [21:39:24:633] [INFO] - (test001.java:24) - ?????????????????????????????????????????????? [21:39:24:633] [INFO] - (test001.java:25) - grou0 整串-> 你好啊,start -> 0 ,end -> 3 [21:39:24:633] [INFO] - (test001.java:26) - grou1 -> 你好啊,start -> 0 ,end -> 3 [21:39:24:633] [INFO] - (test001.java:27) - grou2 -> null,start -> -1 ,end -> -1 [21:39:24:634] [INFO] - (test001.java:28) - grou3 -> null,start -> -1 ,end -> -1 [21:39:24:634] [INFO] - (test001.java:24) - ?????????????????????????????????????????????? [21:39:24:634] [INFO] - (test001.java:25) - grou0 整串-> CCC内容3DDD,start -> 21 ,end -> 30 [21:39:24:634] [INFO] - (test001.java:26) - grou1 -> null,start -> -1 ,end -> -1 [21:39:24:634] [INFO] - (test001.java:27) - grou2 -> null,start -> -1 ,end -> -1 [21:39:24:634] [INFO] - (test001.java:28) - grou3 -> CCC内容3DDD,start -> 21 ,end -> 30 [21:39:24:635] [INFO] - (test001.java:24) - ?????????????????????????????????????????????? [21:39:24:635] [INFO] - (test001.java:25) - grou0 整串-> CCC内容3DDD,start -> 31 ,end -> 40 [21:39:24:635] [INFO] - (test001.java:26) - grou1 -> null,start -> -1 ,end -> -1 [21:39:24:635] [INFO] - (test001.java:27) - grou2 -> null,start -> -1 ,end -> -1 [21:39:24:635] [INFO] - (test001.java:28) - grou3 -> CCC内容3DDD,start -> 31 ,end -> 40 Process finished with exit code 0
可以看出,grou0 是匹配整个串,与grou() 结果相同。上面待匹配字符串中,能被 (你*.?啊)|(AAA.*?BBB)|(CCC.*?DDD) 匹配的字符串有 你好啊、CCC内容3DDD、CCC内容3DDD,所以循环了3次,对应匹配的正则是
(你*.?啊)、(CCC.*?DDD),位置刚好是对应的 grou0、grou3。
参考文章:
https://blog.csdn.net/u011289652/article/details/74474806