正则表达式的match方法疑问

来源:百度知道 编辑:UC知道 时间:2024/05/12 10:05:15
MSN上提供了示例:

string text = "One car red car blue car";
string pat = @"(\w+)\s+(car)";

//string text = "fe*ujn*dfe*";
//string pat = @"[a-z]+(\*)";

// Compile the regular expression.
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

依据你的正则规则string pat = @"(\w+)\s+(car)"; 分组

Group类表示单个捕获组的结果。当与正则表达式匹配的子字符串有多组时,可以使用该类得到某一组的结果。

正则规则string pat = @"(\w+)\s+(car)"; 分组