Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

MatchCollection.Count propriété

 

Date de publication : novembre 2016

Obtient le nombre de correspondances.

Espace de noms:   System.Text.RegularExpressions
Assembly:  System (dans System.dll)

public int Count { get; }

Valeur de propriété

Type: System.Int32

Nombre de correspondances.

Exception Condition
RegexMatchTimeoutException

Un délai d’attente a expiré.

Accessing individual members of the T:System.Text.RegularExpressions.MatchCollection object by retrieving the value of the collection's P:System.Text.RegularExpressions.MatchCollection.Count property causes the regular expression engine to populate the collection using direct evaluation. ln contrast, calling the M:System.Text.RegularExpressions.MatchCollection.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 T:System.Text.RegularExpressions.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 T:System.Text.RegularExpressions.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.

The following example uses the P:System.Text.RegularExpressions.MatchCollection.Count property to determine whether the call to the M:System.Text.RegularExpressions.Regex.Matches(System.String,System.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

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

Plateforme Windows universelle
Disponible depuis 8
.NET Framework
Disponible depuis 1.1
Bibliothèque de classes portable
Pris en charge dans : plateformes .NET portables
Silverlight
Disponible depuis 2.0
Silverlight pour Windows Phone
Disponible depuis 7.0
Windows Phone
Disponible depuis 8.1
Retour au début
Afficher: