Regular capture group (capture group) _ Regular expression

Source: Internet
Author: User
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.
1. Number sequence
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.
(\d{4})-(\d{2}-(\d\d))
1 1 2 3 3 2
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
Reverse reference
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 '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.