. NET regular expressions use the concept of a group of advanced techniques

Source: Internet
Author: User
Tags define datetime expression readline regex expression regular expression
Concept | advanced | skills | The group in regular regular expressions is an important concept, and it is our bridge to advanced regular applications.




the concept of
Group





a regular expression matching result can be divided into several parts, which is the purpose of the group. With the flexibility to use the group, you'll find that the regex is really handy and powerful.





first, let's give 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 = 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 = new DateTime (Year,month,day);


Console.WriteLine (time);


Console.ReadLine ();


}


above example through the group to implement the analysis of a string, and convert it into a DateTime instance, of course, this function with the DateTime.Parse method can be easily implemented.





In this example, I divide the match result into three groups of "Y", "M", and "D" to represent the year, month, and day, respectively, in the form of (?<name>).





Now we have the concept of the group, and then see how to group, very simple, in addition to the approach, we can use a pair of parentheses to define a group, such as the previous 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 = 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 = new DateTime (Year,month,day);


Console.WriteLine (time);


Console.ReadLine ();


}


as you can see from the example above, the first bracket is automatically numbered to 1, followed by the parentheses 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 = 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 = new DateTime (Year,month,day);


Console.WriteLine (time);


Console.ReadLine ();


}


again, we use (?< digital >) to manually give each bracket pairs of group number, (note that I define the position of 1 and 2 not from left to right)





through the three examples above, we know the three ways to define a group for a regex and the corresponding way to refer to the results of groups.





then, for the group definition, there are two points to note:





1, because parentheses are used to define groups, if you want to match "(and"), use "\ (" and "\)" (For definitions of all special characters, see the relevant Regex expression help documentation).





2. If you use the Explicitcapture option when you define a regex, the second example will not succeed because this option requires a group that explicitly defines the number or name to capture and save the result if you do not define the explicitcapture option, and sometimes the class is defined (a | B so the part in the expression, and this (a| B you do not want to capture the results, you can use the "No capture group" syntax, which is defined as (?:) In a way that is directed at (a| B), you can define this to achieve the purpose of not capturing and saving it to the group collection--(?: a| B).





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.