A group in a regular expression is a very important concept. It serves as a bridge to an advanced Regular Expression application.
Group concept
The matching result of a regular expression can be divided into multiple parts, which is the purpose of the Group. After using the group flexibly, you will find that Regex is very convenient and powerful.
Here is an example.
Public static void Main ()
{
String s = "2005-2-21 ";
Regex reg = new Regex (@"(? <Y> \ d {4 })-(? <M> \ d {1, 2 })-(? <D> \ d {1, 2}) ", RegexOptions. Compiled );
Match match = reg. Match (s );
Int year = int. Parse (match. Groups ["y"]. Value );
Int month = int. Parse (match. Groups ["m"]. Value );
Int day = int. Parse (match. Groups ["d"]. Value );
DateTime time = new DateTime (year, month, day );
Console. WriteLine (time );
Console. ReadLine ();
} |
The preceding example analyzes a string through a group and converts it to a DateTime instance. Of course, this function can be easily implemented using the DateTime. Parse method.
In this example, I use (? <Name>.
Now we have the concept of group. Let's look at how to group it. It's very easy. Apart from the above method, we can define a group with a pair of parentheses. For example, we can change the above example:
Public static void Main ()
{
String s = "2005-2-21 ";
Regex reg = new Regex (@ "(\ d {4})-(\ d {1, 2})-(\ d {1, 2})", RegexOptions. Compiled );
Match match = reg. Match (s );
Int year = int. Parse (match. Groups [1]. Value );
Int month = int. Parse (match. Groups [2]. Value );
Int day = int. Parse (match. Groups [3]. Value );
DateTime time = new DateTime (year, month, day );
Console. WriteLine (time );
Console. ReadLine ();
} |
From the above example, we can see that the first bracket is automatically numbered as 1 for the Inclusion group, followed by 2, 3 ......
Public static void Main ()
{
String s = "2005-2-21 ";
Regex reg = new Regex (@"(? <2> \ d {4 })-(? <1> \ d {1, 2 })-(? <3> \ d {1, 2}) ", RegexOptions. Compiled );
Match match = reg. Match (s );
Int year = int. Parse (match. Groups [2]. Value );
Int month = int. Parse (match. Groups [1]. Value );
Int day = int. Parse (match. Groups [3]. Value );
DateTime time = new DateTime (year, month, day );
Console. WriteLine (time );
Console. ReadLine ();
} |
Let's look at the above example. We use (? <Number>) manually number the Group of each bracket pair. (note that the positions 1 and 2 are not defined from left to right)
In the above three examples, We know three methods for defining the Group for Regex and the corresponding method for referencing the Group matching result.
Note the following two points about the Group definition:
1. Because parentheses are used to define groups, to match "(" and ")", use "\ (" and "\)" (for definition of all special characters, see the Regex expression help document ).
2. If the ExplicitCapture option is used when Regex is defined, the second example will not succeed, because this option requires a group with an explicit number or name to capture and save the result, if you do not define the ExplicitCapture option, and sometimes define A class in an expression like (A | B), you do not want to capture the result, then, you can use the "uncaptured group" syntax, which is defined (? :), For (A | B), you can define it in this way to not capture and save it to the Group set --(? : A | B ).