C # Regex group Group "Go"

Source: Internet
Author: User

http://www.cnblogs.com/kiant71/archive/2010/08/14/1799799.html

In a regular expression, if you want to extract several different parts (sub-expression items), you need to use the grouping feature.

In C # regular expressions, the Regex membership is as follows, where group is its grouping processing class.

Regex–> matchecollection (Match item collection)

–> Match (single match content)

–> GroupCollection (the "(Group/Sub-expression Items)" collection contained in a single match)

–> Group ("(Group/Sub-expression items)" content)

–> caputercollection (Group item content display basis?) )

–> Caputer

Group has two access methods for grouping:

1. Array subscript Access

In ((\d+) ([A-z]) \s+ This regular expression contains a total of four groupings, according to the default left-to-right matching method,

Groups[0] represents the match itself, which is the entire expression ((\d+) ([A-z]) \s+

GROUPS[1] Represents the sub-expression item ((\d+) ([A-z])

GROUPS[2] Represents a sub-expression item (\d+)

GROUPS[3] Represents the sub-expression item ([A-z])

1234567891011121314151617181920212223242526272829303132333435363738 stringtext = "1A 2B 3C 4D 5E 6F 7G 8H 9I 10J 11Q 12J 13K 14L 15M 16N ffee80 #800080";Response.Write(text + "<br/>");stringstrPatten = @"((\d+)([a-z]))\s+";Regex rex = newRegex(strPatten, RegexOptions.IgnoreCase);MatchCollection matches = rex.Matches(text);//提取匹配项foreach(Match match inmatches){    GroupCollection groups = match.Groups;    Response.Write(string.Format("<br/>{0} 共有 {1} 个分组:{2}<br/>"                                , match.Value, groups.Count, strPatten));    //提取匹配项内的分组信息    for(inti = 0; i < groups.Count; i++)    {        Response.Write(            string.Format("分组 {0} 为 {1},位置为 {2},长度为 {3}<br/>"                        , i                        , groups[i].Value                        , groups[i].Index                        , groups[i].Length));    }}/*  * 输出: 1A 2B 3C 4D 5E 6F 7G 8H 9I 10J 11Q 12J 13K 14L 15M 16N ffee80 #8000801A 共有 4 个分组:((\d+)([a-z]))\s+分组 0 为 1A ,位置为 0,长度为 3分组 1 为 1A,位置为 0,长度为 2分组 2 为 1,位置为 0,长度为 1分组 3 为 A,位置为 1,长度为 1  ....  */

2. Named access

Use (?<xxx> subexpression) to define a grouping alias so that you can access the group/subexpression content using groups["XXX".

12345678910111213141516171819202122232425 stringtext = "I‘ve found this amazing URL at http://www.sohu.com, and then find ftp://ftp.sohu.comisbetter.";Response.Write(text + "<br/>");stringpattern = @"\b(?<protocol>\S+)://(?<address>\S+)\b";Response.Write(pattern.Replace("<", "&lt;").Replace(">","&gt;") + "<br/><br/>");MatchCollection matches = Regex.Matches(text, pattern);foreach(Match match inmatches){    GroupCollection groups = match.Groups;    Response.Write(string.Format(                    "URL: {0}; Protocol: {1}; Address: {2} <br/>"                    , match.Value                    , groups["protocol"].Value                     , groups["address"].Value));}/*  * 输出 I‘ve found this amazing URL at http://www.sohu.com, and then find ftp://ftp.sohu.comisbetter.    \b(?<protocol>\S+)://(?<address>\S+)\b     URL: http://www.sohu.com; Protocol: http; Address: www.sohu.com     URL: ftp://ftp.sohu.comisbetter; Protocol: ftp; Address: ftp.sohu.comisbetter  */

Content reference from:

C # Regular expression programming (iii): Match class and group class usage http://blog.csdn.net/zhoufoxcn/archive/2010/03/09/5358644.aspx

C # Regular expression class match and group class understanding http://tech.ddvip.com/2008-10/122483707982616.html

C # Regex group Group "Go"

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.