GroupCollection GroupCollection GroupCollection GroupCollection Class

定義

在單一比對中傳回擷取之群組的集合。Returns the set of captured groups in a single match.

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

範例

下列範例會使用正則運算式搭配捕捉群組來解壓縮文字中所使用的商標和注冊商標的相關資訊。The following example uses a regular expression with capturing groups to extract information about trademarks and registered trademarks used in text. 正則運算式模式是\b(\w+?)([\u00AE\u2122]),如下表所示加以解讀。The regular expression pattern is \b(\w+?)([\u00AE\u2122]), which is interpreted as shown in the following table.

模式Pattern 說明Description
\b 尋找字邊界。Look for a word boundary.
(\w+?) 尋找一或多個文字字元。Look for one or more word characters. 這些會一起構成商標名稱。Together, these form the trademarked name. (請注意,這個正則運算式會假設商標包含單一單字)。這是第一個擷取群組。(Note that this regular expression assumes that a trademark consists of a single word.) This is the first capturing group.
([\u00AE\u2122]) 尋找®或™字元。Look for either the ® or the ™ character. 這是第二個擷取群組。This is the second capturing group.

針對每個相符項GroupCollection ,包含Group三個物件。For each match, the GroupCollection contains three Group objects. 第一個物件包含符合整個正則運算式的字串。The first object contains the string that matches the entire regular expression. 第二個物件(代表第一個已捕獲的群組)包含產品名稱。The second object, which represents the first captured group, contains the product name. 第三個物件(代表第二個已捕獲群組)包含商標或註冊商標符號。The third object, which represents the second captured group, contains the trademark or registered trademark symbol.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(\w+?)([\u00AE\u2122])";
      string input = "Microsoft® Office Professional Edition combines several office " +
                     "productivity products, including Word, Excel®, Access®, Outlook®, " +
                     "PowerPoint®, and several others. Some guidelines for creating " +
                     "corporate documents using these productivity tools are available " +
                     "from the documents created using Silverlight™ on the corporate " +
                     "intranet site.";
      
      MatchCollection matches = Regex.Matches(input, pattern);
      foreach (Match match in matches)
      {
         GroupCollection groups = match.Groups;
         Console.WriteLine("{0}: {1}", groups[2], groups[1]);
      }                               
      Console.WriteLine();
      Console.WriteLine("Found {0} trademarks or registered trademarks.", matches.Count);
   }
}
// The example displays the following output:
//       r: Microsoft
//       r: Excel
//       r: Access
//       r: Outlook
//       r: PowerPoint
//       T: Silverlight
Module Example
   Public Sub Main()
      Dim pattern As String = "\b(\w+?)([\u00AE\u2122])"
      Dim input As String = "Microsoft® Office Professional Edition combines several office " + _
                            "productivity products, including Word, Excel®, Access®, Outlook®, " + _
                            "PowerPoint®, and several others. Some guidelines for creating " + _
                            "corporate documents using these productivity tools are available " + _
                            "from the documents created using Silverlight™ on the corporate " + _
                            "intranet site."
      
      Dim matches As MatchCollection = Regex.Matches(input, pattern)
      For Each match As Match In matches
         Dim groups As GroupCollection = match.Groups
         Console.WriteLine("{0}: {1}", groups(2), groups(1))
      Next                               
      Console.WriteLine()
      Console.WriteLine("Found {0} trademarks or registered trademarks.", matches.Count)
   End Sub
End Module
' The example displays the following output:
'       r: Microsoft
'       r: Excel
'       r: Access
'       r: Outlook
'       r: PowerPoint
'       T: Silverlight

備註

類別是以零為基底的集合類別,由一個或多Group個物件所組成,這些物件會提供正則運算式比對中已捕捉群組的相關資訊。 GroupCollectionThe GroupCollection class is a zero-based collection class that consists of one or more Group objects that provide information about captured groups in a regular expression match. 集合是不可變的(唯讀),而且沒有公用的函式。The collection is immutable (read-only) and has no public constructor. Match.Groups屬性會傳回物件。 GroupCollectionA GroupCollection object is returned by the Match.Groups property.

集合包含一或多個System.Text.RegularExpressions.Group物件。The collection contains one or more System.Text.RegularExpressions.Group objects. 如果比對成功,則集合中的第一個元素會包含Group對應至整個相符專案的物件。If the match is successful, the first element in the collection contains the Group object that corresponds to the entire match. 如果正則運算式包含捕捉群組,則每個後續的元素都代表一個已捕捉的群組。Each subsequent element represents a captured group, if the regular expression includes capturing groups. 來自編號(未命名)捕捉群組的比對會依照數值順序出現,然後才符合命名的捕捉群組。Matches from numbered (unnamed) capturing groups appear in numeric order before matches from named capturing groups. 如果比對不成功,則集合會包含單一System.Text.RegularExpressions.Group物件, Success其屬性false為, Value而其String.Empty屬性等於。If the match is unsuccessful, the collection contains a single System.Text.RegularExpressions.Group object whose Success property is false and whose Value property equals String.Empty. 如需詳細資訊,請參閱群組結構一文中的「群組結構和正則運算式物件」一節。For more information, see the "Grouping Constructs and Regular Expression Objects" section in the Grouping Constructs article.

若要逐一查看集合的成員,您應該使用您的語言所提供的集合反復專案結構(例如foreach在C#和For 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. 此外,您可以從Item[Int32]屬性存取個別編號的已捕獲群組(中C#的索引子),而且您可以從Item[String]屬性存取個別的已命名的已捕獲群組。In addition, you can access individual numbered captured groups from the Item[Int32] property (the indexer in C#), and you can access individual named captured groups from the Item[String] property. 請注意,您可以分別呼叫Regex.GetGroupNumbersRegex.GetGroupNames方法,以取出包含所有捕捉群組之數位和名稱的陣列。Note that you can retrieve an array that contains the numbers and names of all capturing groups by calling the Regex.GetGroupNumbers and Regex.GetGroupNames methods, respectively. 這兩者都是實例方法,而且需要您Regex具現化物件,以表示要比對的正則運算式。Both are instance methods and require that you instantiate a Regex object that represents the regular expression to be matched.

屬性

Count Count Count Count

傳回集合中的群組數。Returns the number of groups in the collection.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

取得值,這個值表示集合是否為唯讀。Gets a value that indicates whether the collection is read-only.

IsSynchronized IsSynchronized IsSynchronized IsSynchronized

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

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

以整數索引啟用對集合成員的存取。Enables access to a member of the collection by integer index.

Item[String] Item[String] Item[String] Item[String]

以字串索引啟用對集合成員的存取。Enables access to a member of the collection by string index.

Keys Keys Keys Keys

取得字串列舉,其中包含群組集合的名稱索引鍵。Gets a string enumeration that contains the name keys of the group collection.

SyncRoot SyncRoot SyncRoot SyncRoot

取得可用以同步存取 GroupCollection 的物件。Gets an object that can be used to synchronize access to the GroupCollection.

Values Values Values Values

取得包含群組集合中所有群組的群組列舉。Gets a group enumeration with all the groups in the group collection.

方法

ContainsKey(String) ContainsKey(String) ContainsKey(String) ContainsKey(String)

判斷群組集合是否包含由指定名稱識別的擷取群組。Determines whether the group collection contains a captured group identified by the specified name.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

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

CopyTo(Group[], Int32) CopyTo(Group[], Int32) CopyTo(Group[], Int32) CopyTo(Group[], Int32)

從特定的陣列索引開始,將群組集合的元素複製到 System.Text.RegularExpressions.Group[] 陣列。Copies the elements of the group collection to a System.Text.RegularExpressions.Group[] array, starting at a particular array index.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

(Inherited from Object)
TryGetValue(String, Group) TryGetValue(String, Group) TryGetValue(String, Group) TryGetValue(String, Group)

嘗試擷取由所提供名稱索引鍵識別的群組 (如果存在於群組集合中)。Attempts to retrieve a group identified by the provided name key, if it exists in the group collection.

明確介面實作

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

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

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

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

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

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

ICollection<Group>.Add(Group) ICollection<Group>.Add(Group) ICollection<Group>.Add(Group) ICollection<Group>.Add(Group)

Group 加入集合。Adds a Group to the collection. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

ICollection<Group>.Clear() ICollection<Group>.Clear() ICollection<Group>.Clear() ICollection<Group>.Clear()

清除集合。Clears the collection. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

ICollection<Group>.Contains(Group) ICollection<Group>.Contains(Group) ICollection<Group>.Contains(Group) ICollection<Group>.Contains(Group)

判斷群組集合是否包含特定群組項目。Determines whether the group collection contains a specific group item.

ICollection<Group>.Remove(Group) ICollection<Group>.Remove(Group) ICollection<Group>.Remove(Group) ICollection<Group>.Remove(Group)

從集合移除 GroupRemoves a Group from the collection. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

IEnumerable<Group>.GetEnumerator() IEnumerable<Group>.GetEnumerator() IEnumerable<Group>.GetEnumerator() IEnumerable<Group>.GetEnumerator()

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

IEnumerable<KeyValuePair<String,Group>>.GetEnumerator() IEnumerable<KeyValuePair<String,Group>>.GetEnumerator() IEnumerable<KeyValuePair<String,Group>>.GetEnumerator() IEnumerable<KeyValuePair<String,Group>>.GetEnumerator()

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

IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object)

將物件新增至群組集合。Adds an object to the group collection. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

IList.Clear() IList.Clear() IList.Clear() IList.Clear()

清除集合。Clears the collection. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) IList.Contains(Object)

判斷群組集合是否包含特定群組項目。Determines whether the group collection contains a specific group item.

IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object)

判斷群組集合中特定群組的索引。Determines the index of a specific group in the group collection.

IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object)

Group 插入集合。Inserts a Group into the collection. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize

取得值,指出群組集合是否有固定的大小。Gets a value indicating whether the group collection has a fixed size.

IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] IList.Item[Int32]

取得所需位置中的群組。Gets the group in the desired position.

IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) IList.Remove(Object)

從集合移除 GroupRemoves a Group from the collection. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32)

從集合中移除指定索引處的 GroupRemoves the Group from the collection at the specified index. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

IList<Group>.IndexOf(Group) IList<Group>.IndexOf(Group) IList<Group>.IndexOf(Group) IList<Group>.IndexOf(Group)

判斷群組集合中特定群組的索引。Determines the index of a specific group in the group collection.

IList<Group>.Insert(Int32, Group) IList<Group>.Insert(Int32, Group) IList<Group>.Insert(Int32, Group) IList<Group>.Insert(Int32, Group)

Group 插入集合。Inserts a Group into the collection. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

IList<Group>.Item[Int32] IList<Group>.Item[Int32] IList<Group>.Item[Int32] IList<Group>.Item[Int32]

取得集合中指定位置的群組。Gets the group at the specified position in the collection.

IList<Group>.RemoveAt(Int32) IList<Group>.RemoveAt(Int32) IList<Group>.RemoveAt(Int32) IList<Group>.RemoveAt(Int32)

從集合中移除指定索引處的 GroupRemoves the Group from the collection at the specified index. 呼叫這個方法一律會擲回 NotSupportedExceptionCalling this method always throws NotSupportedException.

擴充方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

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

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱