Group.Captures 屬性

定義

依照最內層最左邊為優先的順序,取得符合擷取群組的所有擷取的集合 (如果規則運算式使用 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). 集合可能有零個或更多項目。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 屬性所傳回的集合會包含單一 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),以從句子中解壓縮單一單字。It defines a regular expression, \b(\w+)\b, that extracts a single word from a sentence. Group 物件會捕捉 "This" 這個字,而 CaptureCollection 中的單一物件包含相同 capture 的相關資訊。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 屬性的實際公用程式,因此群組會在單一正則運算式中捕捉多個子字串。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

適用於

另請參閱