本文整理汇总了C#中System.Text.RegularExpressions.Match.Groups属性的典型用法代码示例。如果您正苦于以下问题:C# Match.Groups属性的具体用法?C# Match.Groups怎么用?C# Match.Groups使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Text.RegularExpressions.Match
的用法示例。
在下文中一共展示了Match.Groups属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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();
}
}
}
输出:
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
示例2: Main
//引入命名空间
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);
Console.WriteLine("Telephone number: {0}", match.Groups[2].Value);
Console.WriteLine();
}
Console.WriteLine();
}
}
输出:
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
示例3: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
public class MainClass{
public static void Main(){
Regex r = new Regex(@"<([^>]+)>([^<]*)</(\1)>");
Match m = r.Match("<M>S</M>");
Console.WriteLine("Tag: {0}, InnerText: {1}", m.Groups[1].Value, m.Groups[2].Value);
}
}