Califique este contenido
Contraer todo/Expandir todo Contraer todo
Esta página es específica de
Microsoft Visual Studio 2008/.NET Framework 3.5

Hay además otras versiones disponibles para:
Biblioteca de clases de .NET Framework
Capture (Clase)

Actualización: Julio de 2008

Representa los resultados de una sola captura de subexpresión. Capture representa una subcadena de una sola captura correcta.

Espacio de nombres:  System.Text.RegularExpressions
Ensamblado:  System (en System.dll)
Visual Basic (Declaración)
<SerializableAttribute> _
Public Class Capture
Visual Basic (Uso)
Dim instance As Capture
C#
[SerializableAttribute]
public class Capture
Visual C++
[SerializableAttribute]
public ref class Capture
J#
/** @attribute SerializableAttribute */ 
public class Capture
JScript
public class Capture

El objeto es inmutable y no tiene constructor público. Las instancias se devuelven mediante la colección devuelta por Captures. Las clases Match y Group extienden Capture.

En el ejemplo siguiente se utilizan objetos Capture para mostrar los miembros de cada grupo de coincidencias de expresiones regulares en la consola.

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]
J#
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.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();
}
// 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]
System..::.Object
  System.Text.RegularExpressions..::.Capture
    System.Text.RegularExpressions..::.Group
Todos los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.

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

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

.NET Framework

Compatible con: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 3.5, 2.0, 1.0

XNA Framework

Compatible con: 2.0, 1.0

Fecha

Historial

Motivo

Julio de 2008

Se ha normalizado el ejemplo y el resultado del ejemplo para todos los lenguajes.

Corrección de errores de contenido.

Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2009 Microsoft Corporation. Reservados todos los derechos. Temas legales | Marcas Registradas | Declaración de privacidad
Page view tracker