MatchCollection.Item[Int32] 속성

정의

컬렉션의 개별 멤버를 가져옵니다.

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

매개 변수

i
Int32

Match 컬렉션의 인덱스입니다.

속성 값

Match

컬렉션의 위치 i에서 캡처된 부분 문자열입니다.

구현

예외

i가 0보다 작거나 Count보다 크거나 같은 경우

시간이 초과되었습니다.

예제

다음 예제에서는 나다니엘 호손 하우스 오브 더 세븐 게이블 의 첫 번째 문장을 구문 분석하고 대문자 또는 소문자 "h"로 시작하는 모든 단어를 포함하는 개체를 반환 MatchCollection 합니다. 그런 다음 각 Item[] 단어를 검색하여 콘솔에 표시하는 데 속성이 사용됩니다.

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

예제의 결과는 다음과 같습니다.

Half  
house  
huge  

설명

C# Item[] 에서 속성은 인덱서이며 코드에서 명시적으로 참조되지 않고 배열인 것처럼 컬렉션에 액세스할 수 있도록 허용합니다 MatchCollection .

일반적으로 개체의 개별 항목은 컬렉션의 MatchCollection 총 항목 수가 속성에서 Count 결정된 후에만 해당 인덱스에서 액세스합니다. 그러나 속성에 Count 액세스하면 정규식 엔진이 직접 평가를 사용하여 컬렉션을 한 번에 빌드합니다. 일반적으로 메서드, C# foreach 문 또는 Visual Basic For Each...Next 문을 사용하여 GetEnumerator 컬렉션을 반복하는 것보다 비용이 많이 듭니다.

개체는 MatchCollection 일반적으로 지연 평가를 사용하여 채워지므로 특정 일치 항목으로 이동하려고 하면 예외가 RegexMatchTimeoutException 발생할 수 있습니다. 일치 작업에 대한 제한 시간 값이 적용되고 특정 일치 항목을 찾으려는 시도가 해당 제한 시간 간격을 초과하는 경우 이 예외가 throw될 수 있습니다.

적용 대상

추가 정보