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. GroupCollection 对象Match.Groups由属性返回。A 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集合将包含一个对象,该对象的false Success属性为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 For Each C#和 .。。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 的浅表副本。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)

向集合中插入 GroupInserts 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)

向集合中插入 GroupInserts 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.

适用于

另请参阅