Uredi

Deli z drugimi prek


MatchCollection.Count Property

Definition

Gets the number of matches.

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

Property Value

The number of matches.

Implements

Exceptions

A time-out occurred.

Examples

The following example uses the Count property to determine whether the call to the Regex.Matches(String, String) method found any matches. If not, it indicates that no matches were found. Otherwise, it enumerates the matches and displays their value and the position in the input string at which they were found.

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

The regular expression pattern \d+ matches one or more decimal characters in an input string.

Remarks

Accessing individual members of the MatchCollection object by retrieving the value of the collection's Count property causes the regular expression engine to populate the collection using direct evaluation. ln contrast, calling the GetEnumerator method (or using the foreach statement in C# and the For Each...Next statement in Visual Basic) causes the regular expression engine to populate the collection on an as needed basis using lazy evaluation. Direct evaluation can be a much more expensive method of building the collection than lazy evaluation.

Because the MatchCollection object is generally populated by using lazy evaluation, trying to determine the number of elements in the collection before it has been fully populated 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 single match exceeds that time-out interval.

Applies to