Share via


Group Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: August 2009

Represents the results from a single capturing group.

Inheritance Hierarchy

System.Object
  System.Text.RegularExpressions.Capture
    System.Text.RegularExpressions.Group
      System.Text.RegularExpressions.Match

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

Syntax

'Declaration
Public Class Group _
    Inherits Capture
public class Group : Capture

The Group type exposes the following members.

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Captures Gets 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 Silverlight for Windows PhoneSupported by Xbox 360 Index The position in the original string where the first character of the captured substring was found. (Inherited from Capture.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Length The length of the captured substring. (Inherited from Capture.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Success Gets a value indicating whether the match is successful.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Value Gets the captured substring from the input string. (Inherited from Capture.)

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows 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 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Gets the captured substring from the input string. (Inherited from Capture.)

Top

Remarks

A capturing group can capture zero, one, or more strings in a single match because of quantifiers. 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.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim pattern As String = "(\b(\w+?)[,:;]?\s?)+[?.!]"
      Dim input As String = "This is one sentence. This is a second sentence."

      Dim match As Match = Regex.Match(input, pattern)
      outputBlock.Text &= "Match: " + match.Value & vbCrLf
      Dim groupCtr As Integer = 0
      For Each group As Group In match.Groups
         groupCtr += 1
         outputBlock.Text += String.Format("   Group {0}: '{1}'", groupCtr, group.Value) & vbCrLf
         Dim captureCtr As Integer = 0
         For Each capture As Capture In group.Captures
            captureCtr += 1
            outputBlock.Text += String.Format("      Capture {0}: '{1}'", captureCtr, capture.Value) & vbCrLf
         Next
      Next
   End Sub
End Module
' 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'
using System;
using System.Text.RegularExpressions;

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

      Match match = Regex.Match(input, pattern);
      outputBlock.Text += "Match: " + match.Value + "\n";
      int groupCtr = 0;
      foreach (Group group in match.Groups)
      {
         groupCtr++;
         outputBlock.Text += String.Format("   Group {0}: '{1}'", groupCtr, group.Value) + "\n";
         int captureCtr = 0;
         foreach (Capture capture in group.Captures)
         {
            captureCtr++;
            outputBlock.Text += String.Format("      Capture {0}: '{1}'", captureCtr, capture.Value) + "\n";
         }
      }
   }
}
// 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'

Version Information

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

Platforms

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

Thread Safety

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

Change History

Date

History

Reason

August 2009

Expanded the Remarks section.

Information enhancement.