Mega Code Archive

 
Categories / C# / Development Class
 

Get CaptureCollection

using System; using System.Text.RegularExpressions; class Example  {    static void Main()     {       string text = "this is is is a test.";       string pat = @"(\w+)\s+(is)";       Regex r = new Regex(pat, RegexOptions.IgnoreCase);       Match m = r.Match(text);       int matchCount = 0;       while (m.Success)        {          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();       }    } }