Asp.net| Skills | regular
A regular expression matching result can be divided into several parts, which is the group.
A match result is divided into groups in the form of (?<name>), examples:
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 ();
}
You can also use encoding to access groups based on the order of the regular inside (). The first bracket is automatically numbered to the inclusion group 1, followed by the parentheses numbered 2, 3 ...
Access mode: Match. GROUPS[1]. Value
Alternatively, you can manually give the group number of each bracket to the (?< number >) method
The depressing thing is that if you don't use regular words for a while, the symbols inside are easy to forget,:-)