This documentation is archived and is not being maintained.

Group.Captures Property

Gets a collection of all the captures matched by the capturing group, in innermost-leftmost-first order (or innermost-rightmost-first order if the regular expression is modified with the RegexOptions.RightToLeft option). The collection may have zero or more items.

Namespace: System.Text.RegularExpressions
Assembly: System (in system.dll)

public CaptureCollection Captures { get; }
/** @property */
public CaptureCollection get_Captures ()

public function get Captures () : CaptureCollection

Not applicable.

Property Value

The collection of substrings matched by the group.

The following example uses the Captures property to capture groups of characters that match a regular expression pattern. In this example, the pattern searches for the group of characters " car". The regular expression pattern contains a backreference to capture the group of characters that precede the matched grouping. The two groups of characters are then stored in a Capture object as Match1, Match2, and so on. The groupings are printed to the console, along with the index position that each character grouping holds in the Capture object.

string text = "One car red car blue car";
string pat = @"(\w+)\s+(car)";
// 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 = 0; j < cc.Count; j++) 
         Capture c = cc[j];
         System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index);
   m = m.NextMatch();
   //This code produces output similar to the following; 
   //results may vary based on the computer/file structure/etc.:
   //Capture0='One', Position=0
   //Capture0='car', Position=4
   //Capture0='red', Position=8
   //Capture0='car', Position=12
   //Capture0='blue', Position=16
   //Capture0='car', Position=21

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0