Groups in regular expressions are a very important concept, and they are our bridge to advanced regular expression applications.
The concept of group.
A regular expression matching result can be divided into multiple parts. This is the purpose of group. After you can use groups flexibly, you will find that Regex is really convenient and powerful.
Let’s take an example first:
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 above example analyzes a string through groups and converts it into a DateTime instance. Of course, this function can be easily implemented using the DateTime.Parse method.
In this example, I use (?<name>) to divide the results of a Match into three groups "y", "m", and "d" representing the year, month, and day respectively.
Now that we have the concept of groups, let’s look at how to group them. It’s very simple. In addition to the above method, we can define a group with a pair of brackets. For example, the above example can be changed to:
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();
}
As can be seen from the above example, the group included in the first bracket pair is automatically numbered 1, and the following brackets are numbered 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();
}
Looking at the above example again, we use (?<number>) to manually number the group of each bracket pair (note that I did not define the positions of 1 and 2 from left to right).
Through the above three examples, we Know the three ways to define Group for Regex and the corresponding ways to reference group matching results.
Then, regarding group definition, please note two more points:
1. Because brackets are used to define groups, so if you want to match "(" and ")", please use "(" and ")" (about all special For the definition of characters, please check the relevant Regex expression help documentation).
2. If the ExplicitCapture option is used when defining Regex, the second example will not succeed, because this option requires an explicitly defined group with a number or name to capture and save the results. If you do not define the ExplicitCapture option, and sometimes If you define an expression that is similar to (A|B), and you don’t want to capture the result of this (A|B), you can use the "non-capturing group" syntax, which is defined as (?:) Method, for (A|B), you can define it like this to achieve the purpose of not capturing and saving it in the Group collection - (?:A|B).