Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Group Class

Represents the results from a single capturing group.

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

[SerializableAttribute]
public class Group : Capture

The Group type exposes the following members.

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCapturesGets a collection of all the captures matched by the capturing group, in innermost-leftmost-first order (or innermost-rightmost-first order if the regular expression is modified with the RegexOptions.RightToLeft option). The collection may have zero or more items.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIndexThe position in the original string where the first character of the captured substring is found. (Inherited from Capture.)
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsLengthGets the length of the captured substring. (Inherited from Capture.)
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsSuccessGets a value indicating whether the match is successful.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsValueGets the captured substring from the input string. (Inherited from Capture.)
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by the XNA FrameworkSynchronizedReturns a Group object equivalent to the one supplied that is safe to share between multiple threads.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringRetrieves the captured substring from the input string by calling the Value property. (Inherited from Capture.)
Top

A capturing group can capture zero, one, or more strings in a single match because of quantifiers. (For more information, see Quantifiers in Regular Expressions.) All the substrings matched by a single capturing group are available from the Group.Captures property. Information about the last substring captured can be accessed directly from the Value and Index properties. (That is, the Group instance is equivalent to the last item of the collection returned by the Captures property, which reflects the last capture made by the capturing group.)

An example helps to clarify this relationship between a Group object and the System.Text.RegularExpressions.CaptureCollection that is returned by the Captures property. The regular expression pattern (\b(\w+?)[,:;]?\s?)+[?.!] matches entire sentences. The regular expression is defined as shown in the following table.

Pattern

Description

\b

Begin the match at a word boundary.

(\w+?)

Match one or more word characters, but as few characters as possible. This is the second (inner) capturing group. (The first capturing group includes the \b language element.)

[,:;]?

Match zero or one occurrence of a comma, colon, or semicolon.

\s?

Match zero or one occurrence of a white-space character.

(\b(\w+?)[,:;]?\s?)+

Match the pattern consisting of a word boundary, one or more word characters, a punctuation symbol, and a white-space character one or more times. This is the first capturing group.

[?.!]

Match any occurrence of a period, question mark, or exclamation point.

In this regular expression pattern, the subpattern (\w+?) is designed to match multiple words within a sentence. However, the value of the Group object represents only the last match that (\w+?) captures, whereas the Captures property returns a CaptureCollection that represents all captured text. As the output shows, the CaptureCollection for the second capturing group contains four objects. The last of these corresponds to the Group object.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(\b(\w+?)[,:;]?\s?)+[?.!]";
      string input = "This is one sentence. This is a second sentence.";

      Match match = Regex.Match(input, pattern);
      Console.WriteLine("Match: " + match.Value);
      int groupCtr = 0;
      foreach (Group group in match.Groups)
      {
         groupCtr++;
         Console.WriteLine("   Group {0}: '{1}'", groupCtr, group.Value);
         int captureCtr = 0;
         foreach (Capture capture in group.Captures)
         {
            captureCtr++;
            Console.WriteLine("      Capture {0}: '{1}'", captureCtr, capture.Value);
         }
      }   
   }
}
// The example displays the following output: 
//       Match: This is one sentence. 
//          Group 1: 'This is one sentence.' 
//             Capture 1: 'This is one sentence.' 
//          Group 2: 'sentence' 
//             Capture 1: 'This ' 
//             Capture 2: 'is ' 
//             Capture 3: 'one ' 
//             Capture 4: 'sentence' 
//          Group 3: 'sentence' 
//             Capture 1: 'This' 
//             Capture 2: 'is' 
//             Capture 3: 'one' 
//             Capture 4: 'sentence'

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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