Java 正则表达式的捕捉组(capture group)

发布日期:2019-07-23 19:48:10 阅读数: 861次 来源: 作者:

捕捉组分为:

  • 通俗捕捉组(Expression)
  • 定名捕捉组(?Expression)

通俗捕捉组

从正则表达式左侧起头,每呈现一个左括号"("记做一个分组,分组编号从 1 起头。0 代表整个表达式。

对于时间字符串:2017-04-25,表达式如下

(\\d{4})-((\\d{2})-(\\d{2}))

有 4 个左括号,所以有 4 个分组:

编号 捕捉组 婚配
0 (\d{4})-((\d{2})-(\d{2})) 2017-04-25
1 (\d{4}) 2017
2 ((\d{2})-(\d{2})) 04-25
3 (\d{2}) 04
4 (\d{2}) 25
public static final String DATE_STRING = "2017-04-25"; public static final String P_COMM = "(\\d{4})-((\\d{2})-(\\d{2}))"; Pattern pattern = Pattern.compile(P_COMM); Matcher matcher = pattern.matcher(DATE_STRING); matcher.find();//必需要有这句 System.out.printf("\nmatcher.group(0) value:%s", matcher.group(0)); System.out.printf("\nmatcher.group(1) value:%s", matcher.group(1)); System.out.printf("\nmatcher.group(2) value:%s", matcher.group(2)); System.out.printf("\nmatcher.group(3) value:%s", matcher.group(3)); System.out.printf("\nmatcher.group(4) value:%s", matcher.group(4));

定名捕捉组

每个以左括号起头的捕捉组,都紧跟着 ?,尔后才是正则表达式。

对于时间字符串:2017-04-25,表达式如下:

(?<year>\\d{4})-(?<md>(?<month>\\d{2})-(?<date>\\d{2}))

有 4 个定名的捕捉组,别离是:

编号 名称 捕捉组 婚配
0 0 (?\d{4})-(?(?\d{2})-(?\d{2})) 2017-04-25
1 year (?\d{4})- 2017
2 md (?(?\d{2})-(?\d{2})) 04-25
3 month (?\d{2}) 04
4 date (?\d{2}) 25

定名的捕捉组同样也能够利用编号获取响应值。

public static final String P_NAMED = "(?<year>\\d{4})-(?<md>(?<month>\\d{2})-(?<date>\\d{2}))"; public static final String DATE_STRING = "2017-04-25"; Pattern pattern = Pattern.compile(P_NAMED); Matcher matcher = pattern.matcher(DATE_STRING); matcher.find(); System.out.printf("\n===========利用名称获取============="); System.out.printf("\nmatcher.group(0) value:%s", matcher.group(0)); System.out.printf("\n matcher.group('year') value:%s", matcher.group("year")); System.out.printf("\nmatcher.group('md') value:%s", matcher.group("md")); System.out.printf("\nmatcher.group('month') value:%s", matcher.group("month")); System.out.printf("\nmatcher.group('date') value:%s", matcher.group("date")); matcher.reset(); System.out.printf("\n===========利用编号获取============="); matcher.find(); System.out.printf("\nmatcher.group(0) value:%s", matcher.group(0)); System.out.printf("\nmatcher.group(1) value:%s", matcher.group(1)); System.out.printf("\nmatcher.group(2) value:%s", matcher.group(2)); System.out.printf("\nmatcher.group(3) value:%s", matcher.group(3)); System.out.printf("\nmatcher.group(4) value:%s", matcher.group(4));

PS:非捕捉组

在左括号后紧跟 ?:,尔后再加上正则表达式,形成非捕捉组 (?:Expression)

对于时间字符串:2017-04-25,表达式如下:

(?:\\d{4})-((\\d{2})-(\\d{2}))

这个正则表达式虽然有四个左括号,理论上有 4 个捕捉组。可是第一组 (?:\d{4}),其实是被忽略的。当利用 matcher.group(4) 时,系统会报错。

编号 捕捉组 婚配
0 (\d{4})-((\d{2})-(\d{2})) 2亚博017-04-25
1 ((\d{2})-(\d{2})) 04-25
2 (\d{2}) 04
3 (\d{2}) 25
public static final String P_UNCAP = "(?:\\d{4})-((\\d{2})-(\\d{2}))"; public static final String DATE_STRING = "2017-04-25"; Pattern pattern = Pattern.compile(P_UNCAP); Matcher matcher = pattern.matcher(DATE_STRING); matcher.find(); System.out.printf("\nmatcher.group(0) value:%s", matcher.group(0)); System.out.printf("\nmatcher.group(1) value:%s", matcher.group(1)); System.out.printf("\nmatcher.group(2) value:%s", matcher.group(2)); System.out.printf("\nmatcher.group(3) value:%s", matcher.group(3)); // Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 4 System.out.printf("\nmatcher.group(4) value:%s", matcher.group(4));

总结

  • 通俗捕捉组利用便利;
  • 定名捕捉组利用清晰;
  • 非捕捉组目前在项目中还没有用武之地。
本文由亚博手机app编辑整理亚博手机app