Export (0) Print
Expand All

GroupCollection Class

Updated: August 2009

Represents a collection of captured groups in a single match.

System.Object
  System.Text.RegularExpressions.GroupCollection

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)

[DefaultMemberAttribute("Item")]
public class GroupCollection : ICollection, 
	IEnumerable

The GroupCollection type exposes the following members.

  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360CountReturns the number of groups in the collection.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsReadOnlyGets a value that indicates whether the collection is read-only.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsSynchronizedGets a value that indicates whether access to the GroupCollection is synchronized (thread-safe).
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360Item[Int32]Enables access to a member of the collection by integer index.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360Item[String]Enables access to a member of the collection by string index.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360SyncRootGets an object that can be used to synchronize access to the GroupCollection.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CopyToCopies all the elements of the collection to the given array beginning at the given index.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetEnumeratorReturns an enumerator that can iterate through the collection.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public Extension MethodSupported by Silverlight for Windows PhoneAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360Cast<TResult>Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360OfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

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 string 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 element whose Success property is false and whose Value property equals String.Empty.

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 characters 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 object contains three Group objects. The first contains the string that matches the entire regular expression. The second, which represents the first captured group, contains the product name. The third, 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 Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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;
         outputBlock.Text += String.Format("{0}: {1}", groups[2], groups[1]) + "\n";
      }
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("Found {0} trademarks or registered trademarks.", matches.Count) + "\n";
   }
}
// The example displays the following output:
//       ®: Microsoft
//       ®: Excel
//       ®: Access
//       ®: Outlook
//       ®: PowerPoint
//       ™: Silverlight


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Date

History

Reason

August 2009

Expanded the Remarks section.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft