MatchCollection.Count 속성

정의

일치하는 항목의 개수를 가져옵니다.

public:
 property int Count { int get(); };
public int Count { get; }
member this.Count : int
Public ReadOnly Property Count As Integer

속성 값

일치하는 항목의 개수입니다.

구현

예외

시간이 초과되었습니다.

예제

다음 예제에서는 사용 합니다 Count 메서드에 대 한 호출이 일치 하는지 여부를 확인 하는 Regex.Matches(String, String) 속성입니다. 그렇지 않은 경우 일치하는 항목이 없음을 나타냅니다. 그렇지 않으면 일치 항목을 열거하고 해당 값과 해당 값이 발견된 입력 문자열의 위치를 표시합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      string[] inputs = { "This sentence contains no numbers.",
                          "123 What do I see?",
                          "2468 369 48 5" };
      foreach (var input in inputs) {
         MatchCollection matches = Regex.Matches(input, pattern);
         Console.WriteLine("Input: {0}", input);
         if (matches.Count == 0)
            Console.WriteLine("   No matches");
         else
            foreach (Match m in matches)
               Console.WriteLine("   {0} at index {1}", m.Value, m.Index);

         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//       Input: This sentence contains no numbers.
//          No matches
//
//       Input: 123 What do I see?
//          123 at index 0
//
//       Input: 2468 369 48 5
//          2468 at index 0
//          369 at index 5
//          48 at index 9
//          5 at index 12
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\d+"
      Dim inputs() As String = { "This sentence contains no numbers.",
                                 "123 What do I see?",
                                 "2468 369 48 5" }
      For Each inputStr In inputs
         Dim matches As MatchCollection = Regex.Matches(inputStr, pattern)
         Console.WriteLine("Input: {0}", inputStr)
         If matches.Count = 0
            Console.WriteLine("   No matches")
         Else
            For Each m As Match In matches
               Console.WriteLine("   {0} at index {1}", m.Value, m.Index)
            Next
         End If
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'       Input: This sentence contains no numbers.
'       No matches
'
'       Input: 123 What do I see?
'       123 at index 0
'
'       Input: 2468 369 48 5
'       2468 at index 0
'       369 at index 5
'       48 at index 9
'       5 at index 12

정규식 패턴 \d+ 은 입력 문자열에서 하나 이상의 10진수 문자와 일치합니다.

설명

컬렉션의 MatchCollection 속성 값을 검색하여 개체의 Count 개별 멤버에 액세스하면 정규식 엔진이 직접 평가를 사용하여 컬렉션을 채웁니다. ln contrast에서 메서드를 호출 GetEnumerator 하거나 C#의 문과 For EachVisual Basic의 ...Next 문을 사용하면 foreach 정규식 엔진이 지연 평가를 사용하여 필요에 따라 컬렉션을 채웁니다. 직접 평가는 지연 평가보다 컬렉션을 빌드하는 훨씬 더 비싼 방법이 될 수 있습니다.

개체는 MatchCollection 일반적으로 지연 계산을 사용하여 채워지므로 완전히 채워지기 전에 컬렉션의 요소 수를 확인하려고 하면 예외가 RegexMatchTimeoutException 발생할 수 있습니다. 일치 작업에 대한 제한 시간 값이 적용되고 단일 일치 항목을 찾으려는 시도가 해당 제한 시간 간격을 초과하는 경우 이 예외가 throw될 수 있습니다.

적용 대상