Export (0) Print
Expand All

MatchCollection.Item Property

Gets an individual member of the collection.

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

public virtual Match this[
	int i
] { get; }

Parameters

i
Type: System.Int32

Index into the Match collection.

Property Value

Type: System.Text.RegularExpressions.Match
The captured substring at position i in the collection.

ExceptionCondition
ArgumentOutOfRangeException

i is less than 0 or greater than or equal to Count.

RegexMatchTimeoutException

A time-out occurred.

In C#, the Item property is an indexer; it is not explicitly referenced in code, but instead allows the MatchCollection collection to be accessed as if it were an array.

Typically, individual items in the MatchCollection object are accessed by their index only after the total number of items in the collection has been determined from the Count property. However, accessing the Count property causes the regular expression engine to use direct evaluation to build the collection all at once. This is typically more expensive than iterating the collection using the GetEnumerator method, the C# foreach statement, or the Visual Basic For Each...Next statement.

Because the MatchCollection object is generally populated by using lazy evaluation, trying to navigate to a specific match may throw a RegexMatchTimeoutException exception. This exception can be thrown if a time-out value for matching operations is in effect, and the attempt to find a specific match exceeds that time-out interval.

The following example parses the first sentence of Nathaniel Hawthorne's House of the Seven Gables and returns a MatchCollection object that contains all words that begin with either an uppercase or lowercase "h". The Item property is then used to retrieve each word and display it to the console.

using System;
using System.Text.RegularExpressions;

public class Class1
{
   public static void Main()
   {   
      string sentence = "Half-way down a by-street of one of our New England towns, stands a rusty wooden " +
                         "house, with seven acutely peaked gables, facing towards various points of the compass, " + 
                         "and a huge, clustered chimney in the midst.";
      string pattern = @"\b[hH]\w*\b"; 
      MatchCollection matches = Regex.Matches(sentence, pattern);
      for (int ctr=0; ctr < matches.Count; ctr++)
      {
         Console.WriteLine(matches[ctr].Value);   
      }   
   }
}

The example produces the following output:

Half
house
huge

.NET Framework

Supported in: 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 Silverlight 8.1, Windows Phone Silverlight 8

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.

Show:
© 2014 Microsoft