.NET Framework Class Library
Capture Class

Updated: July 2008

Represents the results from a single subexpression capture. Capture represents one substring for a single successful capture.

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

Visual Basic (Declaration)
<SerializableAttribute> _
Public Class Capture
Visual Basic (Usage)
Dim instance As Capture
C#
[SerializableAttribute]
public class Capture
Visual C++
[SerializableAttribute]
public ref class Capture
JScript
public class Capture
Remarks

The object is immutable and has no public constructor. Instances are returned through the collection returned by Captures. The Match and Group classes extend Capture.

Examples

The following example uses Capture objects to display the members of each group of regular expression matches to the console.

Visual Basic
Dim text As String = "One fish two fish red fish blue fish"
Dim pat As String = "(?<1>\w+)\s+(?<2>fish)\s*"
' Compile the regular expression.
Dim r As New Regex(pat, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)        
Do While m.Success
   ' Display the first match and its capture set.
   System.Console.WriteLine("Match=[" + m.ToString() + "]")
   Dim cc As CaptureCollection = m.Captures
   Dim c As Capture
   For Each c In  cc
      System.Console.WriteLine("Capture=[" + c.ToString() + "]")
   Next c
   ' Display Group1 and its capture set.
   Dim g1 As Group = m.Groups(1)
   System.Console.WriteLine("Group1=[" + g1.ToString() + "]")
   Dim c1 As Capture
   For Each c1 In  g1.Captures
      System.Console.WriteLine("Capture1=[" + c1.ToString() + "]")
   Next c1
   ' Display Group2 and its capture set.
   Dim g2 As Group = m.Groups(2)
   System.Console.WriteLine("Group2=[" + g2.ToString() + "]")
   Dim c2 As Capture
   For Each c2 In  g2.Captures
      System.Console.WriteLine("Capture2=[" + c2.ToString() + "]")
   Next c2
   ' Advance to the next match.
   m = m.NextMatch()
Loop
' The example displays the following output:
'       Match=[One fish ]
'       Capture=[One fish ]
'       Group1=[One]
'       Capture1=[One]
'       Group2=[fish]
'       Capture2=[fish]
'       Match=[two fish ]
'       Capture=[two fish ]
'       Group1=[two]
'       Capture1=[two]
'       Group2=[fish]
'       Capture2=[fish]
'       Match=[red fish ]
'       Capture=[red fish ]
'       Group1=[red]
'       Capture1=[red]
'       Group2=[fish]
'       Capture2=[fish]
'       Match=[blue fish]
'       Capture=[blue fish]
'       Group1=[blue]
'       Capture1=[blue]
'       Group2=[fish]
'       Capture2=[fish]
C#
string text = "One fish two fish red fish blue fish";
string pat = @"(?<1>\w+)\s+(?<2>fish)\s*";
// 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);
while (m.Success) 
{
   // Display the first match and its capture set.
   System.Console.WriteLine("Match=[" + m + "]");
   CaptureCollection cc = m.Captures;
   foreach (Capture c in cc) 
   {
      System.Console.WriteLine("Capture=[" + c + "]");
   }
   // Display Group1 and its capture set.
   Group g1 = m.Groups[1];
   System.Console.WriteLine("Group1=[" + g1 + "]");
   foreach (Capture c1 in g1.Captures) 
   {
      System.Console.WriteLine("Capture1=[" + c1 + "]");
   }
   // Display Group2 and its capture set.
   Group g2 = m.Groups[2];
   System.Console.WriteLine("Group2=["+ g2 + "]");
   foreach (Capture c2 in g2.Captures) 
   {
      System.Console.WriteLine("Capture2=[" + c2 + "]");
   }
   // Advance to the next match.
   m = m.NextMatch();
}
// The example displays the following output:
//       Match=[One fish ]
//       Capture=[One fish ]
//       Group1=[One]
//       Capture1=[One]
//       Group2=[fish]
//       Capture2=[fish]
//       Match=[two fish ]
//       Capture=[two fish ]
//       Group1=[two]
//       Capture1=[two]
//       Group2=[fish]
//       Capture2=[fish]
//       Match=[red fish ]
//       Capture=[red fish ]
//       Group1=[red]
//       Capture1=[red]
//       Group2=[fish]
//       Capture2=[fish]
//       Match=[blue fish]
//       Capture=[blue fish]
//       Group1=[blue]
//       Capture1=[blue]
//       Group2=[fish]
//       Capture2=[fish]
Visual C++
String^ text = "One fish two fish red fish blue fish";
String^ pat = "(?<1>\\w+)\\s+(?<2>fish)\\s*";

// Compile the regular expression.
Regex^ r = gcnew Regex( pat,RegexOptions::IgnoreCase );

// Match the regular expression pattern against a text string.
Match^ m = r->Match(text);
while ( m->Success )
{
   // Display the first match and its capture set.
   Console::WriteLine( "Match=[{0}]", m );
   CaptureCollection^ cc = m->Captures;
   for each (Capture^ c in cc) 
   {
      System::Console::WriteLine("Capture=[" + c + "]");
   }
   // Display Group1 and its capture set.
   Group^ g1 = m->Groups[ 1 ];
   Console::WriteLine( "Group1=[{0}]", g1 );
   for each (Capture^ c1 in g1->Captures) 
   {
      System::Console::WriteLine("Capture1=[" + c1 + "]");
   }
   // Display Group2 and its capture set.
   Group^ g2 = m->Groups[ 2 ];
   Console::WriteLine( "Group2=[{0}]", g2 );
   for each (Capture^ c2 in g2->Captures) 
   {
      System::Console::WriteLine("Capture2=[" + c2 + "]");
   }
   // Advance to the next match.
   m = m->NextMatch();
}
// The example displays the following output:
//       Match=[One fish ]
//       Capture=[One fish ]
//       Group1=[One]
//       Capture1=[One]
//       Group2=[fish]
//       Capture2=[fish]
//       Match=[two fish ]
//       Capture=[two fish ]
//       Group1=[two]
//       Capture1=[two]
//       Group2=[fish]
//       Capture2=[fish]
//       Match=[red fish ]
//       Capture=[red fish ]
//       Group1=[red]
//       Capture1=[red]
//       Group2=[fish]
//       Capture2=[fish]
//       Match=[blue fish]
//       Capture=[blue fish]
//       Group1=[blue]
//       Capture1=[blue]
//       Group2=[fish]
//       Capture2=[fish]
JScript
var text : String = "One fish two fish red fish blue fish";
var pat : String = "(?<1>\\w+)\\s+(?<2>fish)\\s*";
// Compile the regular expression.
var r : Regex = new Regex(pat, RegexOptions.IgnoreCase);
// Match the regex pattern against a text string
var m : Match = r.Match(text);
while (m.Success) {
    // Display the first match and its capture set.
    System.Console.WriteLine("Match=[" + m + "]");
    var cc : CaptureCollection = m.Captures;
    for (var c : Capture in cc) {
        System.Console.WriteLine("Capture=[" + c + "]");
    }
    // Display Group1 and its capture set.
    var g1 : Group = m.Groups[1];
    System.Console.WriteLine("Group1=[" + g1 + "]");
    for (var c1 : Capture in g1.Captures) {
        System.Console.WriteLine("Capture1=[" + c1 + "]");
    }
    // Display Group2 and its capture set.
    var g2 : Group = m.Groups[2];
    System.Console.WriteLine("Group2=["+ g2 + "]");
    for (var c2 : Capture in g2.Captures) {
        System.Console.WriteLine("Capture2=[" + c2 + "]");
    }
    // Advance to the next match.
    m = m.NextMatch();
}
// The example displays the following output:
//       Match=[One fish ]
//       Capture=[One fish ]
//       Group1=[One]
//       Capture1=[One]
//       Group2=[fish]
//       Capture2=[fish]
//       Match=[two fish ]
//       Capture=[two fish ]
//       Group1=[two]
//       Capture1=[two]
//       Group2=[fish]
//       Capture2=[fish]
//       Match=[red fish ]
//       Capture=[red fish ]
//       Group1=[red]
//       Capture1=[red]
//       Group2=[fish]
//       Capture2=[fish]
//       Match=[blue fish]
//       Capture=[blue fish]
//       Group1=[blue]
//       Capture1=[blue]
//       Group2=[fish]
//       Capture2=[fish]
Inheritance Hierarchy

System..::.Object
  System.Text.RegularExpressions..::.Capture
    System.Text.RegularExpressions..::.Group
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Change History

Date

History

Reason

July 2008

Standardized the example and example output for all languages.

Content bug fix.

Tags :


Page view tracker