Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

Capture Class

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)

[SerializableAttribute] 
public class Capture
/** @attribute SerializableAttribute() */ 
public class Capture
SerializableAttribute 
public class Capture
Not applicable.

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.

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

string text = "One car red car blue car";
string pat = @"(?<1>\w+)\s+(?<2>car)\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();
}

String text = "One car red car blue car";
String pat = "(?<1>\\w+)\\s+(?<2>car)\\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.get_Success()) {
    // Display the first match and its capture set.
    System.Console.WriteLine("Match=[" + m + "]");
    CaptureCollection cc = m.get_Captures();
    for (int iCtr = 0; iCtr < cc.get_Count(); iCtr++) {
        Capture c = (Capture)cc.get_Item(iCtr);
        System.Console.WriteLine("Capture=[" + c + "]");
    }

    // Display Group1 and its capture set.
    Group g1 = m.get_Groups().get_Item(1);
    System.Console.WriteLine("Group1=[" + g1 + "]");
    for (int iCtr = 0; iCtr < g1.get_Captures().get_Count(); iCtr++) {
        Capture c1 = (Capture)g1.get_Captures().get_Item(iCtr);
        System.Console.WriteLine("Capture1=[" + c1 + "]");
    }

    // Display Group2 and its capture set.
    Group g2 = m.get_Groups().get_Item(2);
    System.Console.WriteLine("Group2=[" + g2 + "]");
    for (int iCtr = 0; iCtr < g2.get_Captures().get_Count(); iCtr++) {
        Capture c2 = (Capture)g2.get_Captures().get_Item(iCtr);
        System.Console.WriteLine("Capture2=[" + c2 + "]");
    }

    // Advance to the next match.
    m = m.NextMatch();
}

var text : String = "One car red car blue car";
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 produces 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]
System.Object
  System.Text.RegularExpressions.Capture
     System.Text.RegularExpressions.Group
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.