Windows apps
Collapse the table of content
Expand the table of content
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.

MatchCollection Class

Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string.

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

'Declaration
<SerializableAttribute> _
Public Class MatchCollection _
	Implements ICollection, IEnumerable
'Usage
Dim instance As MatchCollection

The collection is immutable (read-only) and has no public constructor. The Regex.Matches method returns a MatchCollection object.

When applying a regular expression pattern to a particular input string, the regular expression engine uses either of two techniques to build the MatchCollection object:

  • Direct evaluation.

    The MatchCollection object is populated all at once, with all matches resulting from a particular call to the Regex.Matches method. This technique is used when the collection's Count property is accessed. It typically is the more expensive method of populating the collection and entails a greater performance hit.

  • Lazy evaluation.

    The MatchCollection object is populated as needed on a match-by-match basis. It is equivalent to the regular expression engine calling the Regex.Match method repeatedly and adding each match to the collection. This technique is used when the collection is accessed through its GetEnumerator method, or when it is accessed using the foreach statement (in C#) or the For Each...Next statement (in Visual Basic).

The following example code illustrates the use of the MatchCollection class to interrogate a set of Match instances.

Imports System
Imports System.Text.RegularExpressions

Public Module Test

    Public Sub Main()
        ' Define a regular expression for repeated words. 
        Dim rx As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _
               RegexOptions.Compiled Or RegexOptions.IgnoreCase)

        ' Define a test string.         
        Dim text As String = "The the quick brown fox  fox jumped over the lazy dog dog." 

        ' Find matches. 
        Dim matches As MatchCollection = rx.Matches(text)

        ' Report the number of matches found.
        Console.WriteLine("{0} matches found in:", matches.Count)
        Console.WriteLine("   {0}", text)

        ' Report on each match. 
        For Each match As Match In matches
            Dim groups As GroupCollection = match.Groups
            Console.WriteLine("'{0}' repeated at positions {1} and {2}", _ 
                              groups.Item("word").Value, _
                              groups.Item(0).Index, _
                              groups.Item(1).Index)
        Next 
    End Sub 
End Module 
' The example produces the following output to the console: 
'       3 matches found in: 
'          The the quick brown fox  fox jumped over the lazy dog dog. 
'       'The' repeated at positions 0 and 4 
'       'fox' repeated at positions 20 and 25 
'       'dog' repeated at positions 50 and 54

System.Object
  System.Text.RegularExpressions.MatchCollection

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

Show:
© 2017 Microsoft