Match.Groups Property
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Gets a collection of groups matched by the regular expression.
Namespace: System.Text.RegularExpressions
Assembly: System (in System.dll)
Syntax
'Declaration
Public Overridable ReadOnly Property Groups As GroupCollection
public virtual GroupCollection Groups { get; }
Property Value
Type: System.Text.RegularExpressions.GroupCollection
The character groups matched by the pattern.
Remarks
A regular expression pattern can include subexpressions, which are defined by enclosing a portion of the regular expression pattern in parentheses. Every such subexpression forms a group. For example, the regular expression pattern (\d{3})-(\d{3}-\d{4}), which matches North American telephone numbers, has two subexpressions. The first consists of the area code, which composes the first three digits of the telephone number. This group is captured by the first portion of the regular expression, (\d{3}).The second consists of the individual telephone number, which composes the last seven digits of the telephone number. This group is captured by the second portion of the regular expression, (\d{3}-\d{4}). These two groups can then be retrieved from the GroupCollection object that is returned by the Groups property, as the following example shows.
Imports System.Text.RegularExpressions
Module Example
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
Dim pattern As String = "(\d{3})-(\d{3}-\d{4})"
Dim input As String = "212-555-6666 906-932-1111 415-222-3333 425-888-9999"
Dim matches As MatchCollection = Regex.Matches(input, pattern)
For Each match As Match In matches
outputBlock.Text += String.Format("Area Code: {0}", match.Groups(1).Value) & vbCrLf
outputBlock.Text += String.Format("Telephone number: {0}", match.Groups(2).Value) & vbCrLf
outputBlock.Text &= vbCrLf
Next
outputBlock.Text &= vbCrLf
End Sub
End Module
' The example displays the following output:
' Area Code: 212
' Telephone number: 555-6666
'
' Area Code: 906
' Telephone number: 932-1111
'
' Area Code: 415
' Telephone number: 222-3333
'
' Area Code: 425
' Telephone number: 888-9999
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
string pattern = @"(\d{3})-(\d{3}-\d{4})";
string input = "212-555-6666 906-932-1111 415-222-3333 425-888-9999";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
outputBlock.Text += String.Format("Area Code: {0}", match.Groups[1].Value) + "\n";
outputBlock.Text += String.Format("Telephone number: {0}", match.Groups[2].Value) + "\n";
outputBlock.Text += "\n";
}
outputBlock.Text += "\n";
}
}
// The example displays the following output:
// Area Code: 212
// Telephone number: 555-6666
//
// Area Code: 906
// Telephone number: 932-1111
//
// Area Code: 415
// Telephone number: 222-3333
//
// Area Code: 425
// Telephone number: 888-9999
The GroupCollection object returned by the Match.Groups property always has at least one member. If the regular expression engine cannot find any matches in a particular input string, the Group.Success property of the single Group object in the collection is set to False and the Group object's Value property is set to String.Empty. If the regular expression engine can find a match, the first element of the GroupCollection object returned by the Groups property contains a string that matches the entire regular expression pattern.
Examples
The following example attempts to match a regular expression pattern against a sample string. The example uses the Groups property to store information retrieved by the match for display to the console.
Imports System.Text.RegularExpressions
Module Example
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
Dim text As String = "One car red car blue car"
Dim pat As String = "(\w+)\s+(car)"
' Instantiate the regular expression object.
Dim r As Regex = New Regex(pat, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim matchcount As Integer = 0
Do While m.Success
matchcount += 1
outputBlock.Text += "Match" & (matchcount) & vbCrLf
Dim i As Integer
For i = 1 To 2
Dim g As Group = m.Groups(i)
outputBlock.Text += "Group" & i & "='" & g.ToString() & "'" & vbCrLf
Dim cc As CaptureCollection = g.Captures
Dim j As Integer
For j = 0 To cc.Count - 1
Dim c As Capture = cc(j)
outputBlock.Text += "Capture" & j & "='" & c.ToString() _
& "', Position=" & c.Index & vbCrLf
Next
Next
m = m.NextMatch()
Loop
End Sub
End Module
' This example displays the following output:
' Match1
' Group1='One'
' Capture0='One', Position=0
' Group2='car'
' Capture0='car', Position=4
' Match2
' Group1='red'
' Capture0='red', Position=8
' Group2='car'
' Capture0='car', Position=12
' Match3
' Group1='blue'
' Capture0='blue', Position=16
' Group2='car'
' Capture0='car', Position=21
using System;
using System.Text.RegularExpressions;
class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
string text = "One car red car blue car";
string pat = @"(\w+)\s+(car)";
// Compile the regular expression.
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
int matchCount = 0;
while (m.Success)
{
outputBlock.Text += "Match" + (++matchCount) + "\n";
for (int i = 1; i <= 2; i++)
{
Group g = m.Groups[i];
outputBlock.Text += "Group" + i + "='" + g + "'" + "\n";
CaptureCollection cc = g.Captures;
for (int j = 0; j < cc.Count; j++)
{
Capture c = cc[j];
outputBlock.Text += "Capture" + j + "='" + c + "', Position=" + c.Index + "\n";
}
}
m = m.NextMatch();
}
}
}
//This example displays the following output:
// Match1
// Group1='One'
// Capture0='One', Position=0
// Group2='car'
// Capture0='car', Position=4
// Match2
// Group1='red'
// Capture0='red', Position=8
// Group2='car'
// Capture0='car', Position=12
// Match3
// Group1='blue'
// Capture0='blue', Position=16
// Group2='car'
// Capture0='car', Position=21
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.