Condividi tramite


MatchCollection.Item Property

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

Gets an individual member of the collection.

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

Syntax

'Declaration
Public Overridable ReadOnly Property Item ( _
    i As Integer _
) As Match
public virtual Match this[
    int i
] { get; }

Parameters

Property Value

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

Exceptions

Exception Condition
ArgumentOutOfRangeException

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

Remarks

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.

Examples

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.

Option Strict On

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim pattern As String = "\b[hH]\w*\b"
      Dim sentence As 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."
      Dim matches As MatchCollection = Regex.Matches(sentence, pattern)
      For ctr As Integer = 0 To matches.Count - 1
         outputBlock.Text &= matches.Item(ctr).Value & vbCrLf
      Next
   End Sub
End Module
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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++)
      {
         outputBlock.Text += matches[ctr].Value + "\n";
      }
   }
}

The example produces the following output:

Half
house
huge

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.