Condividi tramite


MatchCollection Class

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

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

Inheritance Hierarchy

System.Object
  System.Text.RegularExpressions.MatchCollection

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

Syntax

'Declaration
<DefaultMemberAttribute("Item")> _
Public Class MatchCollection _
    Implements ICollection, IEnumerable
[DefaultMemberAttribute("Item")]
public class MatchCollection : ICollection, 
    IEnumerable

The MatchCollection type exposes the following members.

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Count Gets the number of matches.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsReadOnly Gets a value that indicates whether the collection is read only.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsSynchronized Gets a value indicating whether access to the collection is synchronized (thread-safe).
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Item Gets an individual member of the collection.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 SyncRoot Gets an object that can be used to synchronize access to the collection.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CopyTo Copies all the elements of the collection to the given array starting at the given index.
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 GetEnumerator Provides an enumerator in the same order as Item.
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 Returns a string that represents the current object. (Inherited from Object.)

Top

Extension Methods

  Name Description
Public Extension MethodSupported by Silverlight for Windows Phone AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360 OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)

Top

Remarks

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

Examples

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

Imports System.Text.RegularExpressions

Public Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' Define a regular expression for repeated words.
      Dim rx As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _
                          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.
      outputBlock.Text &= String.Format("{0} matches found in:", matches.Count) & vbCrLf
      outputBlock.Text &= String.Format("   {0}", text) & vbCrLf

      ' Report on each match.
      For Each match As Match In matches
         Dim groups As GroupCollection = match.Groups
         outputBlock.Text &= String.Format("'{0}' repeated at positions {1} and {2}", _
                           groups.Item("word").Value, _
                           groups.Item(0).Index, _
                           groups.Item(1).Index) & vbCrLf
      Next
   End Sub
End Module
' The example produces the following output:
'       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
using System;
using System.Text.RegularExpressions;

public class Example
{

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      // Define a regular expression for repeated words.
      Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
                           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.
      outputBlock.Text += String.Format("{0} matches found in:\n   {1}",
                        matches.Count,
                        text) + "\n";

      // Report on each match.
      foreach (Match match in matches)
      {
         GroupCollection groups = match.Groups;
         outputBlock.Text += String.Format("'{0}' repeated at positions {1} and {2}",
                           groups["word"].Value,
                           groups[0].Index,
                           groups[1].Index) + "\n";
      }

   }

}
// The example produces the following output:
//       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

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.