There are two forms of capturing groups
One kind is the common capturing group, does not produce the ambiguity the case, after short name captures the group, the grammar rule: (expression);
The other is named capture Group, syntax rules: (? <name>expression) or (?) Name ' expression, the two are equivalent.
If you do not explicitly name a capturing group, that is, if you do not use a named Capture group, you need to access all capturing groups in numerical order
In the case of a normal capturing group, the number of the capturing group is numbered from left to right in the order in which they appear.
The above regular expression can be used to match the date formatted as YYYY-MM-DD, in order to differentiate in the following table, using the \d{2} and \d\d two ways
There is also a group with a default number of 0, representing the whole of the regular expression
Match string with the above regular expression: 2008-12-31
The result of the match is:
Number |
Named |
Capturing groups |
Matching content |
0 |
|
(\d{4})-(\d{2}-(\d\d)) |
2008-12-31 |
1 |
|
(\d{4}) |
2008 |
2 |
|
(\d{2}-(\d\d)) |
12-31 |
3 |
|
(\d\d) |
31 |
If you explicitly name a group, which is named a capturing group, the captured content can be referenced by the group name
However, if the regular expression uses both a common capturing group and a named capture group, the number of the capturing group is particularly noteworthy, and the number rule is to number the normal capturing group first and then the named Capture Group
(\d{4})-(? <date>\d{2}-(\d\d))
1 1 3 2
Match string with the above regular expression: 2 008-12-31
matches the result:
Number |
Named |
Capturing groups |
Matching content |
0 |
|
(\d{4})-(\d{2}-(\d\d)) |
2008-12-31 |
1 |
|
(\d{4}) |
2008 |
2 |
|
(\d\d) |
31 |
3 |
Date |
(? <date>\d{2}-(\d\d)) |
12-31 |
2, capturing the group's reference
References to capturing groups generally have the following
A) in a regular expression, a reference to what is captured by the previous capturing group, called a reverse reference
b) in regular expressions, (? expression) true|false), a conditional expression
C in the program, a reference to capturing the contents of the capture group
For a reference to a common capturing group, the syntax rule is: \K<NUM>, usually abbreviated to \num, where num is a decimal number, that is, the number of the capturing group
For a reference to a named capture group, the syntax rule is:\k<name> or \k ' name '