正则表达式match和group的区别 具有相同模式的字符串使用组的提
一、案例: Match类 示例:查找出字符串中包含的url string text = "FirstUrl: http://www.sohu.com,SecondUrl: http://www.baidu.com "; 结果:
Group类 示例:找到字符串中包含的url,并找出每个url的协议和域名地址 @"b(?<protocol>S+)://(?<address>S+)b"; //匹配URL的模式,并分组 Console.WriteLine("文本中包含的URL地址有:"); Console.Read(); 说明:"?<protocol>"和"?<address>"定义了每个组的别名protocol和address (注意:这是一个match内的设置不同名称的组,对这些组进行提取) (参考:http://www.52php.cn/article/p-toaotdxm-vg.htmlhttp://www.52php.cn/article/p-ftgbstus-vg.html)
正则表达式模式可以包含子表达式,这些子表达式是通过将正则表达式模式的一部分用括号括起来定义的。每个这样的子表达式构成一个组。 Groups 属性提供对有关这些子表达式匹配的信息。 例如,与北美电话号码匹配的正则表达式模式(d{3})-(d{3}-d{4}) 有两个子表达式。 第一组由区号构成,它包含电话号码的前三位数字。此组由正则表达式的第一部分(d{3}) 捕获。 第二组由单个电话号码组成,它包含电话号码的后七位数字。此组由正则表达式的第二部分(d{3}-d{4}) 捕获。 然后,可以从由Groups 属性返回的 GroupCollection 对象来检索这两个组,如以下示例所示。 using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = @"(d{3})-(d{3}-d{4})"; string input = "212-555-6666 906-932-1111 415-222-3333 425-888-9999"; MatchCollection matches = Regex.Matches(input,pattern); foreach (Match match in matches) { Console.WriteLine("Area Code: {0}",match.Groups[1].Value);//字符串内第一个匹配match的第一个组(d{3}) Console.WriteLine("Telephone number: {0}",match.Groups[2].Value); Console.WriteLine(); } Console.WriteLine(); } } // The example displays the following output: // Area Code: 212 // Telephone number: 555-6666 // // Area Code: 906 // Telephone number: 932-1111 // // Area Code: 415 // Telephone number: 222-3333 // // Area Code: 425 // Telephone number: 888-9999 如果正则表达式引擎在特定的输入字符串中找不到任何匹配项,则集合中的单个Group 对象的Group.Success 属性将设置为false,且 Group 对象的Value 属性将设置为String.Empty。 如果正则表达式引擎可以找到匹配项,Groups 属性返回的GroupCollection 对象的第一个元素将包含一个与整个正则表达式模式匹配的字符串。 如果正则表达式包含捕获组,则每个后续元素都表示一个被捕获组。有关详细信息,请参阅“分组构造和正则表达式对象”来区分这篇正则表达式中的分组构造 文章。 下面的示例尝试将正则表达式模式与示例字符串进行匹配。此示例将Groups 属性用于存储要在控制台上显示的匹配项所检索的信息。 using System; using System.Text.RegularExpressions; class Example { static void Main() { string text = "One car red car blue car"; string pat = @"(w+)s+(car)"; // Instantiate the regular expression object. Regex r = new Regex(pat,RegexOptions.IgnoreCase); // Match the regular expression pattern against a text string. Match m = r.Match(text); int matchCount = 0; while (m.Success) { Console.WriteLine("Match"+ (++matchCount)); for (int i = 1; i <= 2; i++) { Group g = m.Groups[i]; Console.WriteLine("Group"+i+"='" + g + "'"); CaptureCollection cc = g.Captures; for (int j = 0; j < cc.Count; j++) { Capture c = cc[j]; System.Console.WriteLine("Capture"+j+"='" + c + "',Position="+c.Index); } } m = m.NextMatch(); } } } // This example displays the following output: // Match1 // Group1='One' // Capture0='One',Position=0 // Group2='car' // Capture0='car',Position=4 // Match2 // Group1='red' // Capture0='red',Position=8 // Group2='car' // Capture0='car',Position=12 // Match3 // Group1='blue' // Capture0='blue',Position=16 // Group2='car' // Capture0='car',Position=21
三、分组构造和正则表达式对象 由正则表达式捕获组匹配的子字符串由 System.Text.RegularExpressions.Group 对象表示,其从 System.Text.RegularExpressions.GroupCollection 对象检索,其由 Match.Groups 属性返回。填充 GroupCollection 对象,如下所示: ?下一组 Group 对象表示未命名(编号)的捕获组。它们以在正则表达式中定义的顺序出现,从左至右。这些组的索引值范围从 1 到集合中未命名捕获组的数目。(特定组索引等效于其带编号的反向引用。有关向后引用的更多信息,请参见正则表达式中的反向引用构造。) ?最后的 Group 对象组表示命名的捕获组。它们以在正则表达式中定义的顺序出现,从左至右。第一个名为捕获组的索引值是一个大于最后一个未命名的捕获组的索引。如果正则表达式中没有未命名捕获组,则第一个命名的捕获组的索引值为零。
下面的示例阐释 Group 和 Capture 对象之间的关系。 using System; using System.Text.RegularExpressions; public class Example { public static void Main() { string pattern = @"(b(w+)W+)+"; string input = "This is a short sentence."; Match match = Regex.Match(input,pattern); Console.WriteLine("Match: '{0}'",match.Value); for (int <span style="color:#ff0000;">ctr = 1</span>; ctr < match.Groups.Count; ctr++) { Console.WriteLine(" Group {0}: '{1}'",ctr,match.Groups[ctr].Value);//<span style="color:#ff0000;">ctr = 1开始match内部数组检索</span> int capCtr = 0; foreach (Capture capture in match.Groups[ctr].Captures)//<span style="color:#ff0000;">ctr = 0开始</span> { Console.WriteLine(" Capture {0}: '{1}'",capCtr,capture.Value); capCtr++; } } } } // The example displays the following output: // Match: 'This is a short sentence. ' // Group 1: 'sentence.' // Capture 0: 'This ' // Capture 1: 'is ' // Capture 2: 'a ' // Capture 3: 'short ' // Capture 4: 'sentence.' // Group 2: 'sentence' // Capture 0: 'This' // Capture 1: 'is' // Capture 2: 'a' // Capture 3: 'short' // Capture 4: 'sentence'
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |