Group.Captures Group.Captures Group.Captures Group.Captures Property

定義

内側と左側が先の順序で、キャプチャ グループに一致したすべてのキャプチャ結果のコレクションを取得します。または、正規表現が RightToLeft オプションを使用して変更されている場合は、内側と右端が先の順序で取得します。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 RightToLeft option). このコレクションには 0 個以上の項目が格納されています。The collection may have zero or more items.

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

プロパティ値

グループに一致した部分文字列のコレクション。The collection of substrings matched by the group.

注釈

量指定子がキャプチャに適用されていない場合のグループ、によって返されるコレクション、Capturesプロパティには、1 つが含まれています。Captureとして同じ部分文字列の情報を提供するオブジェクト、Groupオブジェクト。If a quantifier is not applied to a capturing group, the collection returned by the Captures property contains a single Capture object that provides information about the same substring as the Group object. このことを次の例で説明します。This is illustrated in the following example. 正規表現を定義\b(\w+)\b、1 つの word の文から抽出します。It defines a regular expression, \b(\w+)\b, that extracts a single word from a sentence. Groupオブジェクトは 1 つのオブジェクトと"This"という単語をキャプチャ、CaptureCollection同じキャプチャについての情報が含まれています。The Group object captures the word "This", and the single object in the CaptureCollection contains information about the same capture.

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

実際のユーティリティ、Capturesプロパティは、グループが 1 つの正規表現で複数の部分文字列をキャプチャするため、量指定子がキャプチャ グループに適用されるときに発生します。The real utility of the Captures property occurs when a quantifier is applied to a capturing group so that the group captures multiple substrings in a single regular expression. ここで、Groupオブジェクトは、一方にキャプチャされた最後の部分文字列に関する情報を格納、Capturesプロパティには、グループによってキャプチャされたすべての部分文字列の情報が含まれています。In this case, the Group object contains information about the last captured substring, whereas the Captures property contains information about all the substrings captured by the group. 次の例では、正規表現で\b(\w+\s*)+\.ピリオドで終わる文全体に一致します。In the following example, the regular expression \b(\w+\s*)+\. matches an entire sentence that ends in a period. グループ(\w+\s*)+コレクション内の個々 の単語をキャプチャします。The group (\w+\s*)+ captures the individual words in the collection. Groupコレクションにのみ、最後のキャプチャされた部分文字列に関する情報が含まれていますでは、文中「文の」最後の単語をキャプチャします。Because the Group collection contains information only about the last captured substring, it captures the last word in the sentence, "sentence". ただし、グループによってキャプチャされた単語がによって返されるコレクションから利用可能なCapturesプロパティ。However, each word captured by the group is available from the collection returned by the Captures property.

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

適用対象

こちらもご覧ください