Asp. NET application skills: the definition of regular learning group and the way of reference

Source: Internet
Author: User
Tags definition datetime reference regular expression
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,:-)



Related Article

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.