Share via


Group.Captures Property

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

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.

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

Syntax

'Declaration
Public ReadOnly Property Captures As CaptureCollection
public CaptureCollection Captures { get; }

Property Value

Type: System.Text.RegularExpressions.CaptureCollection
The collection of substrings matched by the group.

Remarks

If a quantifier is not applied to a capturing group, the collection returned by the Captures property contains a single Capture object that provides information about the same substring as the Group object. This is illustrated in the following example. It defines a regular expression, \b(\w+)\b, that extracts a single word from a sentence. The Group object captures the word "This", and the single object in the CaptureCollection contains information about the same capture.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim pattern As String = "\b(\w+)\b"
      Dim input As String = "This is one sentence."
      Dim match As Match = Regex.Match(input, pattern)
      If match.Success Then
         outputBlock.Text += String.Format("Matched text: {0}", match.Value) & vbCrLf
         For ctr As Integer = 1 To match.Groups.Count - 1
            outputBlock.Text += String.Format("   Group {0}:  {1}", ctr, match.Groups(ctr).Value) & vbCrLf
            Dim captureCtr As Integer = 0
            For Each capture As Capture In match.Groups(ctr).Captures
               outputBlock.Text += String.Format("      Capture {0}: {1}", _
                                 captureCtr, capture.Value) & vbCrLf
               captureCtr += 1
            Next
         Next
      End If
   End Sub
End Module
' The example displays the following output:
'       Matched text: This
'          Group 1:  This
'             Capture 0: This
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string pattern = @"\b(\w+)\b";
      string input = "This is one sentence.";
      Match match = Regex.Match(input, pattern);
      if (match.Success)
      {
         outputBlock.Text += String.Format("Matched text: {0}", match.Value) + "\n";
         for (int ctr = 1; ctr <= match.Groups.Count - 1; ctr++)
         {
            outputBlock.Text += String.Format("   Group {0}:  {1}", ctr, match.Groups[ctr].Value) + "\n";
            int captureCtr = 0;
            foreach (Capture capture in match.Groups[ctr].Captures)
            {
               outputBlock.Text += String.Format("      Capture {0}: {1}",
                                 captureCtr, capture.Value) + "\n";
               captureCtr += 1;
            }
         }
      }
   }
}
// The example displays the following output:
//       Matched text: This
//          Group 1:  This
//             Capture 0: This

The real utility of the Captures property occurs when a quantifier is applied to a capturing group so that the group captures multiple substrings in a single regular expression. In this case, the Group object contains information about the last captured substring, whereas the Captures property contains information about all the substrings captured by the group. In the following example, the regular expression \b(\w+\s*)+\. matches an entire sentence that ends in a period. The group (\w+\s*)+ captures the individual words in the collection. Because the Group collection contains information only about the last captured substring, it captures the last word in the sentence, "sentence". However, each word captured by the group is available from the collection returned by the Captures property.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim input As String = "This is a sentence. This is another sentence."
      Dim pattern As String = "\b(\w+\s*)+\."
      Dim match As Match = Regex.Match(input, pattern)
      If match.Success Then
         outputBlock.Text += String.Format("Matched text: {0}", match.Value) & vbCrLf
         For ctr As Integer = 1 To match.Groups.Count - 1
            outputBlock.Text += String.Format("   Group {0}:  {1}", ctr, match.Groups(ctr).Value) & vbCrLf
            Dim captureCtr As Integer = 0
            For Each capture As Capture In match.Groups(ctr).Captures
               outputBlock.Text += String.Format("      Capture {0}: {1}", _
                                 captureCtr, capture.Value) & vbCrLf
               captureCtr += 1
            Next
         Next
      End If
   End Sub
End Module
' The example displays the following output:
'       Matched text: This is a sentence.
'          Group 1:  sentence
'             Capture 0: This
'             Capture 1: is
'             Capture 2: a
'             Capture 3: sentence
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string input = "This is a sentence. This is another sentence.";
      string pattern = @"\b(\w+\s*)+\.";
      Match match = Regex.Match(input, pattern);
      if (match.Success)
      {
         outputBlock.Text += String.Format("Matched text: {0}", match.Value) + "\n";
         for (int ctr = 1; ctr < match.Groups.Count; ctr++)
         {
            outputBlock.Text += String.Format("   Group {0}:  {1}", ctr, match.Groups[ctr].Value) + "\n";
            int captureCtr = 0;
            foreach (Capture capture in match.Groups[ctr].Captures)
            {
               outputBlock.Text += String.Format("      Capture {0}: {1}",
                                 captureCtr, capture.Value) + "\n";
               captureCtr++;
            }
         }
      }
   }
}
// The example displays the following output:
//       Matched text: This is a sentence.
//          Group 1:  sentence
//             Capture 0: This
//             Capture 1: is
//             Capture 2: a
//             Capture 3: 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.