GroupCollection Class

Definition

Returns the set of captured groups in a single match.

public class GroupCollection : System.Collections.ICollection
Inheritance
GroupCollection
Implements

Inherited Members

System.Object

Examples

The following example uses a regular expression with capturing groups to extract information about trademarks and registered trademarks used in text. 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.

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

Remarks

The 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. A GroupCollection object is returned by the Match.Groups property.

The collection contains one or more System.Text.RegularExpressions.Group objects. 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. 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.

To 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 in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method. 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. 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. Both are instance methods and require that you instantiate a Regex object that represents the regular expression to be matched.

Properties

Count

Returns the number of groups in the collection.

IsReadOnly

Gets a value that indicates whether the collection is read-only.

IsSynchronized

Gets a value that indicates whether access to the GroupCollection is synchronized (thread-safe).

Item[Int32]

Enables access to a member of the collection by integer index.

Item[String]

Enables access to a member of the collection by string index.

SyncRoot

Gets an object that can be used to synchronize access to the GroupCollection.

Methods

CopyTo(Array, Int32)

Copies all the elements of the collection to the given array beginning at the given index.

CopyTo(Group[], Int32)
GetEnumerator()

Provides an enumerator that iterates through the collection.

Explicit Interface Implementations

ICollection<Group>.Add(Group)
ICollection<Group>.Clear()
ICollection<Group>.Contains(Group)
ICollection<Group>.Remove(Group)
IEnumerable<Group>.GetEnumerator()
IList<Group>.IndexOf(Group)
IList<Group>.Insert(Int32, Group)
IList<Group>.Item[Int32]
IList<Group>.RemoveAt(Int32)
ICollection.CopyTo(Array, Int32)
ICollection.IsSynchronized
ICollection.SyncRoot
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)

Extension Methods

Cast<TResult>(IEnumerable)
OfType<TResult>(IEnumerable)
AsParallel(IEnumerable)
AsQueryable(IEnumerable)