แก้ไข

แชร์ผ่าน


MatchCollection.Item[Int32] Property

Definition

Gets an individual member of the collection.

public:
 virtual property System::Text::RegularExpressions::Match ^ default[int] { System::Text::RegularExpressions::Match ^ get(int i); };
public virtual System.Text.RegularExpressions.Match this[int i] { get; }
member this.Item(int) : System.Text.RegularExpressions.Match
Default Public Overridable ReadOnly Property Item(i As Integer) As Match

Parameters

i
Int32

Index into the Match collection.

Property Value

The captured substring at position i in the collection.

Implements

Exceptions

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

A time-out occurred.

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.

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);   
      }   
   }
}
Option Strict On

Imports System.Text.RegularExpressions

Module TestMatches
   Public Sub Main()
      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
         Console.WriteLine(matches.Item(ctr).Value)
      Next           
   End Sub
End Module

The example produces the following output:

Half
house
huge

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.

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.

Applies to

See also