Group.Captures Özellik

Tanım

Yakalama grubuyla eşleşen tüm yakalamaların bir koleksiyonunu, en soldaki en iç birinci sırada (veya normal ifade seçeneğiyle RightToLeft değiştirildiğinde en içteki-en sağdaki-ilk sırada) alır. Koleksiyonda sıfır veya daha fazla öğe olabilir.

public:
 property System::Text::RegularExpressions::CaptureCollection ^ Captures { System::Text::RegularExpressions::CaptureCollection ^ get(); };
public System.Text.RegularExpressions.CaptureCollection Captures { get; }
member this.Captures : System.Text.RegularExpressions.CaptureCollection
Public ReadOnly Property Captures As CaptureCollection

Özellik Değeri

CaptureCollection

Grup tarafından eşleşen alt dize koleksiyonu.

Açıklamalar

Bir niceleyici bir yakalama grubuna uygulanmazsa, özelliği tarafından Captures döndürülen koleksiyon, nesneyle aynı alt dize Group hakkında bilgi sağlayan tek Capture bir nesne içerir. Bu, aşağıdaki örnekte gösterilmiştir. Bir tümceden tek bir sözcük ayıklayan normal bir ifade \b(\w+)\btanımlar. nesnesi Group "This" sözcüğünü yakalar ve içindeki CaptureCollection tek nesne aynı yakalama hakkında bilgi içerir.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(\w+)\b";
      string input = "This is one sentence.";
      Match match = Regex.Match(input, pattern);
      if (match.Success) {
         Console.WriteLine("Matched text: {0}", match.Value);
         for (int ctr = 1; ctr <= match.Groups.Count - 1; ctr++) {
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups[ctr].Value);
            int captureCtr = 0;
            foreach (Capture capture in match.Groups[ctr].Captures) {
               Console.WriteLine("      Capture {0}: {1}", 
                                 captureCtr, capture.Value);
               captureCtr += 1;                  
            }
         }
      }   
   }
}
// The example displays the following output:
//       Matched text: This
//          Group 1:  This
//             Capture 0: This
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(\w+)\b"
      Dim input As String = "This is one sentence."
      Dim match As Match = Regex.Match(input, pattern)
      If match.Success Then
         Console.WriteLine("Matched text: {0}", match.Value)
         For ctr As Integer = 1 To match.Groups.Count - 1
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups(ctr).Value)
            Dim captureCtr As Integer = 0
            For Each capture As Capture In match.Groups(ctr).Captures
               Console.WriteLine("      Capture {0}: {1}", _
                                 captureCtr, capture.Value)
               captureCtr += 1                  
            Next
         Next
      End If   
   End Sub
End Module
' The example displays the following output:
'       Matched text: This
'          Group 1:  This
'             Capture 0: This

Özelliğin Captures gerçek yardımcı programı, bir yakalama grubuna bir miktar niceleyici uygulandığında gerçekleşir, böylece grup tek bir normal ifadede birden çok alt dize yakalar. Bu durumda, Group nesne yakalanan son alt dize hakkında bilgi içerirken Captures özelliği grup tarafından yakalanan tüm alt dizeler hakkında bilgi içerir. Aşağıdaki örnekte normal ifade \b(\w+\s*)+\. , nokta ile biten tümceyle eşleşir. Grup (\w+\s*)+ , koleksiyondaki tek tek sözcükleri yakalar. Group Koleksiyon yalnızca yakalanan son alt dize hakkında bilgi içerdiğinden, cümlenin son sözcüğü olan "cümle" sözcüğünü yakalar. Ancak, grup tarafından yakalanan her sözcük özelliği tarafından Captures döndürülen koleksiyondan kullanılabilir.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is a sentence. This is another sentence.";
      string pattern = @"\b(\w+\s*)+\.";
      Match match = Regex.Match(input, pattern);
      if (match.Success) {
         Console.WriteLine("Matched text: {0}", match.Value);
         for (int ctr = 1; ctr < match.Groups.Count; ctr++) {
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups[ctr].Value);
            int captureCtr = 0;
            foreach (Capture capture in match.Groups[ctr].Captures) {
               Console.WriteLine("      Capture {0}: {1}", 
                                 captureCtr, capture.Value);
               captureCtr++; 
            }
         }
      }   
   }
}
// The example displays the following output:
//       Matched text: This is a sentence.
//          Group 1:  sentence
//             Capture 0: This
//             Capture 1: is
//             Capture 2: a
//             Capture 3: sentence
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "This is a sentence. This is another sentence."
      Dim pattern As String = "\b(\w+\s*)+\."
      Dim match As Match = Regex.Match(input, pattern)
      If match.Success Then
         Console.WriteLine("Matched text: {0}", match.Value)
         For ctr As Integer = 1 To match.Groups.Count - 1
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups(ctr).Value)
            Dim captureCtr As Integer = 0
            For Each capture As Capture In match.Groups(ctr).Captures
               Console.WriteLine("      Capture {0}: {1}", _
                                 captureCtr, capture.Value)
               captureCtr += 1                  
            Next
         Next
      End If   
   End Sub
End Module
' The example displays the following output:
'       Matched text: This is a sentence.
'          Group 1:  sentence
'             Capture 0: This
'             Capture 1: is
'             Capture 2: a
'             Capture 3: sentence

Şunlara uygulanır

Ayrıca bkz.