CaptureCollection 類別

定義

代表單一擷取群組完成的擷取集合。Represents the set of captures made by a single capturing group.

public ref class CaptureCollection : System::Collections::ICollection
[System.Serializable]
public class CaptureCollection : System.Collections.ICollection
type CaptureCollection = class
    interface ICollection
    interface IEnumerable
Public Class CaptureCollection
Implements ICollection
繼承
CaptureCollection
屬性
實作

範例

下列範例會比較 Group.CapturesMatch.Captures 屬性所傳回之 CaptureCollection 物件中的 Capture 物件。The following example compares the Capture objects in the CaptureCollection object returned by the Group.Captures and Match.Captures properties. 它也會比較 Group 物件與 Group.Captures 屬性所傳回 CaptureCollection 中的 Capture 物件。It also compares Group objects with the Capture objects in the CaptureCollection returned by the Group.Captures property. 此範例會使用下列兩個正則運算式,在單一輸入字串中尋找相符專案:The example uses the following two regular expressions to find matches in a single input string:

  • \b\w+\W{1,2}

    這個正則運算式模式會識別一個單字,其中包含一或多個文字字元,後面接著一或兩個非文字字元,例如空白字元或標點符號。This regular expression pattern identifies a word that consists of one or more word characters, followed by either one or two non-word characters such as white space or punctuation. 正則運算式不包含任何捕捉群組。The regular expression does not include any capturing groups. 範例的輸出顯示 Group.CapturesMatch.Captures 屬性所傳回的 Match 物件和 CaptureCollection 物件都包含相同相符項的相關資訊。The output from the example shows that both the Match object and the CaptureCollection objects returned by the Group.Captures and Match.Captures properties contain information about the same match.

  • (\b\w+\W{1,2})+

    這個正則運算式模式會識別句子中的單字。This regular expression pattern identifies the words in a sentence. 此模式會定義單一的「捕獲群組」,其中包含一或多個文字字元,後面接著一或兩個非文字字元。The pattern defines a single capturing group that consists of one or more word characters followed by one or two non-word characters. 正則運算式模式會使用 + 數量詞來符合這個群組的一或多個專案。The regular expression pattern uses the + quantifier to match one or more occurrences of this group. 這個範例的輸出顯示 Match.Captures 屬性所傳回的 Match 物件和 CaptureCollection 物件包含相同相符項的相關資訊。The output from this example shows that the Match object and the CaptureCollection object returned by the Match.Captures property contain information about the same match. 第二個 Group 物件,它會對應到正則運算式中唯一的「捕捉」群組,只會識別最後一個已捕獲的字串,而第一個「捕獲」群組的 Group.Captures 屬性所傳回的 CaptureCollection 物件則包含所有已捕捉的子字串。The second Group object, which corresponds to the only capturing group in the regular expression, identifies only the last captured string, whereas the CaptureCollection object returned by the first capturing group's Group.Captures property includes all captured substrings.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern;  
      string input = "The young, hairy, and tall dog slowly walked across the yard.";
      Match match;

      // Match a word with a pattern that has no capturing groups.
      pattern = @"\b\w+\W{1,2}";
      match = Regex.Match(input, pattern);
      Console.WriteLine("Pattern: " + pattern);
      Console.WriteLine("Match: " + match.Value);
      Console.WriteLine("  Match.Captures: {0}", match.Captures.Count);
      for (int ctr = 0; ctr < match.Captures.Count; ctr++)
         Console.WriteLine("    {0}: '{1}'", ctr, match.Captures[ctr].Value);
      Console.WriteLine("  Match.Groups: {0}", match.Groups.Count);
      for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
      {
         Console.WriteLine("    Group {0}: '{1}'", 
                           groupCtr, match.Groups[groupCtr].Value);
         Console.WriteLine("    Group({0}).Captures: {1}", 
                           groupCtr, match.Groups[groupCtr].Captures.Count);
         for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
            Console.WriteLine("      Capture {0}: '{1}'", 
                              captureCtr, 
                              match.Groups[groupCtr].Captures[captureCtr].Value);
      }
      Console.WriteLine("-----\n");

      // Match a sentence with a pattern that has a quantifier that 
      // applies to the entire group.
      pattern = @"(\b\w+\W{1,2})+";
      match = Regex.Match(input, pattern);
      Console.WriteLine("Pattern: " + pattern);
      Console.WriteLine("Match: " + match.Value);
      Console.WriteLine("  Match.Captures: {0}", match.Captures.Count);
      for (int ctr = 0; ctr < match.Captures.Count; ctr++)
         Console.WriteLine("    {0}: '{1}'", ctr, match.Captures[ctr].Value);
      
      Console.WriteLine("  Match.Groups: {0}", match.Groups.Count);
      for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
      {
         Console.WriteLine("    Group {0}: '{1}'", groupCtr, match.Groups[groupCtr].Value);
         Console.WriteLine("    Group({0}).Captures: {1}", 
                           groupCtr, match.Groups[groupCtr].Captures.Count);
         for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
            Console.WriteLine("      Capture {0}: '{1}'", captureCtr, match.Groups[groupCtr].Captures[captureCtr].Value);
      }
   }
}
// The example displays the following output:
//    Pattern: \b\w+\W{1,2}
//    Match: The
//      Match.Captures: 1
//        0: 'The '
//      Match.Groups: 1
//        Group 0: 'The '
//        Group(0).Captures: 1
//          Capture 0: 'The '
//    -----
//    
//    Pattern: (\b\w+\W{1,2})+
//    Match: The young, hairy, and tall dog slowly walked across the yard.
//      Match.Captures: 1
//        0: 'The young, hairy, and tall dog slowly walked across the yard.'
//      Match.Groups: 2
//        Group 0: 'The young, hairy, and tall dog slowly walked across the yard.'
//        Group(0).Captures: 1
//          Capture 0: 'The young, hairy, and tall dog slowly walked across the yard.'
//        Group 1: 'yard.'
//        Group(1).Captures: 11
//          Capture 0: 'The '
//          Capture 1: 'young, '
//          Capture 2: 'hairy, '
//          Capture 3: 'and '
//          Capture 4: 'tall '
//          Capture 5: 'dog '
//          Capture 6: 'slowly '
//          Capture 7: 'walked '
//          Capture 8: 'across '
//          Capture 9: 'the '
//          Capture 10: 'yard.'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String  
      Dim input As String = "The young, hairy, and tall dog slowly walked across the yard."
      Dim match As Match

      ' Match a word with a pattern that has no capturing groups.
      pattern = "\b\w+\W{1,2}"
      match = Regex.Match(input, pattern)
      Console.WriteLine("Pattern: " + pattern)
      Console.WriteLine("Match: " + match.Value)
      Console.WriteLine("  Match.Captures: {0}", match.Captures.Count)
      For ctr As Integer = 0 To match.Captures.Count - 1
         Console.WriteLine("    {0}: '{1}'", ctr, match.Captures(ctr).Value)
      Next
      Console.WriteLine("  Match.Groups: {0}", match.Groups.Count)
      For groupCtr As Integer = 0 To match.Groups.Count - 1
         Console.WriteLine("    Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value)
         Console.WriteLine("    Group({0}).Captures: {1}", _
                           groupCtr, match.Groups(groupCtr).Captures.Count)
         For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
            Console.WriteLine("      Capture {0}: '{1}'", _
                              captureCtr, _
                              match.Groups(groupCtr).Captures(captureCtr).Value)
         Next
      Next
      Console.WriteLine("-----")
      Console.WriteLine()

      ' Match a sentence with a pattern that has a quantifier that 
      ' applies to the entire group.
      pattern = "(\b\w+\W{1,2})+"
      match = Regex.Match(input, pattern)
      Console.WriteLine("Pattern: " + pattern)
      Console.WriteLine("Match: " + match.Value)
      Console.WriteLine("  Match.Captures: {0}", match.Captures.Count)
      For ctr As Integer = 0 To match.Captures.Count - 1
         Console.WriteLine("    {0}: '{1}'", ctr, match.Captures(ctr).Value)
      Next
      Console.WriteLine("  Match.Groups: {0}", match.Groups.Count)
      For groupCtr As Integer = 0 To match.Groups.Count - 1
         Console.WriteLine("    Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value)
         Console.WriteLine("    Group({0}).Captures: {1}", _
                           groupCtr, match.Groups(groupCtr).Captures.Count)
         For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
            Console.WriteLine("      Capture {0}: '{1}'", captureCtr, match.Groups(groupCtr).Captures(captureCtr).Value)
         Next
      Next
   End Sub
End Module
' The example displays the following output:
'    Pattern: \b\w+\W{1,2}
'    Match: The
'      Match.Captures: 1
'        0: 'The '
'      Match.Groups: 1
'        Group 0: 'The '
'        Group(0).Captures: 1
'          Capture 0: 'The '
'    -----
'    
'    Pattern: (\b\w+\W{1,2})+
'    Match: The young, hairy, and tall dog slowly walked across the yard.
'      Match.Captures: 1
'        0: 'The young, hairy, and tall dog slowly walked across the yard.'
'      Match.Groups: 2
'        Group 0: 'The young, hairy, and tall dog slowly walked across the yard.'
'        Group(0).Captures: 1
'          Capture 0: 'The young, hairy, and tall dog slowly walked across the yard.'
'        Group 1: 'yard.'
'        Group(1).Captures: 11
'          Capture 0: 'The '
'          Capture 1: 'young, '
'          Capture 2: 'hairy, '
'          Capture 3: 'and '
'          Capture 4: 'tall '
'          Capture 5: 'dog '
'          Capture 6: 'slowly '
'          Capture 7: 'walked '
'          Capture 8: 'across '
'          Capture 9: 'the '
'          Capture 10: 'yard.'

備註

集合是不可變的(唯讀),而且沒有公用的函式。The collection is immutable (read-only) and has no public constructor. CaptureCollection 物件包含一或多個 Capture 物件。The CaptureCollection object contains one or more Capture objects.

下列屬性會傳回 CaptureCollection 類別的實例:Instances of the CaptureCollection class are returned by the following properties:

  • Group.Captures 屬性。The Group.Captures property. 集合的每個成員都代表一個由「捕獲群組」所捕獲的子字串。Each member of the collection represents a substring captured by a capturing group. 如果未將數量詞套用至捕捉群組,則 CaptureCollection 包含單一 Capture 物件,其代表與 Group 物件相同的已捕捉子字串。If a quantifier is not applied to a capturing group, the CaptureCollection includes a single Capture object that represents the same captured substring as the Group object. 如果將數量詞套用至捕捉群組,則 CaptureCollection 會針對每個已捕獲的子字串包含一個 Capture 物件,而 Group 物件只會提供最後一個已捕獲之子字串的相關資訊。If a quantifier is applied to a capturing group, the CaptureCollection includes one Capture object for each captured substring, and the Group object provides information only about the last captured substring.

  • Match.Captures 屬性。The Match.Captures property. 在此情況下,集合是由單一 Capture 物件所組成,提供整個相符的相關資訊。In this case, the collection consists of a single Capture object that provides information about the match as a whole. 也就是說,CaptureCollection 物件會提供與 Match 物件相同的資訊。That is, the CaptureCollection object provides the same information as the Match object.

若要逐一查看集合的成員,您應該使用您的語言所提供的集合反復專案結構(例如中C#的 foreachFor Each...NextTo iterate through the members of the collection, you should use the collection iteration construct provided by your language (such as foreach in C# and For EachNext 在 Visual Basic),而不是抓取 GetEnumerator 方法所傳回的列舉值。in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method.

屬性

Count

取得群組所擷取的子字串數目。Gets the number of substrings captured by the group.

IsReadOnly

取得值,指出集合是否為唯讀。Gets a value that indicates whether the collection is read only.

IsSynchronized

取得值,該值指出是否同步存取集合 (安全執行緒)。Gets a value that indicates whether access to the collection is synchronized (thread-safe).

Item[Int32]

取得集合的個別成員。Gets an individual member of the collection.

SyncRoot

取得物件,這個物件可以用來對集合進行同步存取。Gets an object that can be used to synchronize access to the collection.

方法

CopyTo(Array, Int32)

複製集合的所有項目至指定索引處開始的指定陣列。Copies all the elements of the collection to the given array beginning at the given index.

CopyTo(Capture[], Int32)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

提供逐一查看集合的列舉值。Provides an enumerator that iterates through the collection.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.CopyTo(Array, Int32)

複製集合的所有項目至指定索引處開始的指定陣列。Copies all the elements of the collection to the given array beginning at the given index.

ICollection.IsSynchronized

取得值,該值指出是否同步存取集合 (安全執行緒)。Gets a value that indicates whether access to the collection is synchronized (thread-safe).

ICollection.SyncRoot

取得物件,這個物件可以用來對集合進行同步存取。Gets an object that can be used to synchronize access to the collection.

ICollection<Capture>.Add(Capture)
ICollection<Capture>.Clear()
ICollection<Capture>.Contains(Capture)
ICollection<Capture>.Remove(Capture)
IEnumerable<Capture>.GetEnumerator()
IList.Add(Object)
IList.Clear()
IList.Contains(Object)
IList.IndexOf(Object)
IList.Insert(Int32, Object)
IList.IsFixedSize
IList.Item[Int32]
IList.Remove(Object)
IList.RemoveAt(Int32)
IList<Capture>.IndexOf(Capture)
IList<Capture>.Insert(Int32, Capture)
IList<Capture>.Item[Int32]
IList<Capture>.RemoveAt(Int32)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱