一、案例:
Match类
示例:查找出字符串中包含的url
string text = "FirstUrl: ,SecondUrl: ";string pattern =@"\b(\S+)://(\S+)\b"; //匹配URL的模式MatchCollectionmc = Regex.Matches(text, pattern); //满足pattern的匹配集合Console.WriteLine("文本中包含的URL地址有:");foreach (Matchmatch in mc){ Console.WriteLine(match.Value);}Console.ReadLine();
结果:
Group类
示例:找到字符串中包含的url,并找出每个url的协议和域名地址
string text = "FirstUrl: ,SecondUrl: ";string pattern =@"\b(?<protocol>\S+)://(?<address>\S+)\b"; //匹配URL的模式,并分组MatchCollection mc = Regex.Matches(text, pattern); //满足pattern的匹配集合
Console.WriteLine("文本中包含的URL地址有:");foreach (Match match in mc){ GroupCollection gc = match.Groups; string outputText = "URL:" + match.Value + ";Protocol:" +gc["protocol"].Value+ ";Address:" +gc["address"].Value; Console.WriteLine(outputText);}
Console.Read();
说明:"?<protocol>"和"?<address>"定义了每个组的别名protocol和address
(注意:这是一个match内的设置不同名称的组,对这些组进行提取)
(参考:)
二、原理
Groups 属性提供对有关这些子表达式匹配的信息。
例如,与北美电话号码匹配的正则表达式模式(\d{3})-(\d{3}-\d{4}) 有两个子表达式。
捕获。
第二组由单个电话号码组成,它包含电话号码的后七位数字。此组由正则表达式的第二部分(\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
由 Match.Groups 属性返回的GroupCollection 对象始终至少具有一个成员。
如果正则表达式引擎在特定的输入字符串中找不到任何匹配项,则集合中的单个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
三、分组构造和正则表达式对象
人之所以能,是相信能。