Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

GroupCollection Class

Updated: August 2009

Represents a collection of captured groups in a single match.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)
[SerializableAttribute]
public class GroupCollection : ICollection, 
	IEnumerable

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. 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.

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.

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
System.Object
  System.Text.RegularExpressions.GroupCollection
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

August 2009

Further expanded the Remarks section.

Customer feedback.

December 2008

Expanded the Remarks section and added an example.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.