Capture groups are divided into:
- Ordinary capturing groups (Expression)
- Named capturing groups (?Expression)
Ordinary Capture Groups
Starting from the left side of the regex, each occurrence of a left parenthesis "(" is counted as a group, with group numbers starting from 1. 0 represents the entire expression.
For the time string: 2017-04-25, the expression is as follows
(\\d{4})-((\\d{2})-(\\d{2}))
There are 4 left parentheses, so there are 4 groups:
| Number |
Capture group |
Match |
| 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));
Named Capture Groups
Each capture group starting with a left parenthesis is immediately followed by?, and then the regex expression.
For the time string: 2017-04-25, the expression is as follows:
(?<year>\\d{4})-(?<md>(?<month>\\d{2})-(?<date>\\d{2}))
There are 4 named capture groups, namely:
| Number |
Name |
Capture group |
Match |
| 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 |
Named capture groups can also use numbers to get the corresponding values.
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===========Get by name=============");
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===========Get by number=============");
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: Non-capturing Groups
Immediately after the left parenthesis, follow with?:, and then add the regex expression to form a non-capturing group.(?:Expression)。
For the time string: 2017-04-25, the expression is as follows:
(?:\\d{4})-((\\d{2})-(\\d{2}))
Although this regex has four left parentheses, theoretically there are 4 capture groups. But the first group(?:\d{4}), is actually ignored. When using matcher.group(4), the system will throw an error.
| Number |
Capture group |
Match |
| 0 |
(\d{4})-((\d{2})-(\d{2})) |
2017-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));
System.out.printf("\nmatcher.group(4) value:%s", matcher.group(4));
Summary
- Ordinary capture groups are convenient to use;
- Named capture groups are clear to use;
- Non-capturing groups currently have no use in projects.
Original URL: https://blog.csdn.net/just4you/article/details/70767928