Export (0) Print
Expand All

MatchCollection Class

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

System.Object
  System.Text.RegularExpressions.MatchCollection

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

[SerializableAttribute]
public class MatchCollection : ICollection, 
	IEnumerable

The MatchCollection type exposes the following members.

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCountGets the number of matches.
Public propertySupported by the XNA FrameworkIsReadOnlyGets a value that indicates whether the collection is read only.
Public propertySupported by the XNA FrameworkIsSynchronizedGets a value indicating whether access to the collection is synchronized (thread-safe).
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsItemGets an individual member of the collection.
Public propertySupported by the XNA FrameworkSyncRootGets an object that can be used to synchronize access to the collection.
Top

  NameDescription
Public methodSupported by the XNA FrameworkCopyToCopies all the elements of the collection to the given array starting at the given index.
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 appsGetEnumeratorProvides an enumerator that iterates through the collection.
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 methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public Extension MethodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.CopyToCopies all the elements of the collection to the specified array starting at the specified index.
Explicit interface implemetationPrivate propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.IsSynchronizedGets a value that indicates whether access to the collection is synchronized (thread-safe).
Explicit interface implemetationPrivate propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.SyncRootGets an object that can be used to synchronize access to the collection.
Top

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

The collection contains zero or more System.Text.RegularExpressions.Match objects. If the match is successful, the collection is populated with one System.Text.RegularExpressions.Match object for each match found in the input string. If the match is unsuccessful, the collection contains no System.Text.RegularExpressions.Match objects, and its Count property equals zero.

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).

To iterate through the members of the collection, you should use the collection iteration construct provided by your language (such as foreach in C# and For EachNext in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method.

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

using System;
using System.Text.RegularExpressions;

public class Test
{

    public static void Main ()
    {

        // Define a regular expression for repeated words.
        Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Define a test string.         
        string text = "The the quick brown fox  fox jumped over the lazy dog dog.";

        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found in:\n   {1}", 
                          matches.Count, 
                          text);

        // Report on each match. 
        foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                              groups["word"].Value, 
                              groups[0].Index, 
                              groups[1].Index);
        }

    }
	
}
// 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

.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