RegexCompilationInfo.MatchTimeout Właściwość

Definicja

Pobiera lub ustawia domyślny interwał limitu czasu wyrażenia regularnego.

public:
 property TimeSpan MatchTimeout { TimeSpan get(); void set(TimeSpan value); };
public TimeSpan MatchTimeout { get; set; }
member this.MatchTimeout : TimeSpan with get, set
Public Property MatchTimeout As TimeSpan

Wartość właściwości

Domyślny maksymalny interwał czasu, który może upłynąć w operacji dopasowania wzorca przed wyrzuceniem RegexMatchTimeoutException wartości lub InfiniteMatchTimeout jeśli limity czasu są wyłączone.

Przykłady

W poniższym przykładzie zdefiniowano jedno skompilowane wyrażenie regularne o nazwie DuplicateChars , które identyfikuje co najmniej dwa wystąpienia tego samego znaku w ciągu wejściowym. Skompilowane wyrażenie regularne ma domyślny limit czasu 2 sekund. Po wykonaniu przykładu zostanie utworzona biblioteka klas o nazwie RegexLib.dll zawierająca skompilowane wyrażenie regularne.

using System;
using System.Reflection;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
        // Match two or more occurrences of the same character.
        string pattern = @"(\w)\1+";
        
        // Use case-insensitive matching. 
        var rci = new RegexCompilationInfo(pattern, RegexOptions.IgnoreCase,
                                           "DuplicateChars", "CustomRegexes", 
                                           true, TimeSpan.FromSeconds(2));

        // Define an assembly to contain the compiled regular expression.
        var an = new AssemblyName();
        an.Name = "RegexLib";
        RegexCompilationInfo[] rciList = { rci };

        // Compile the regular expression and create the assembly.
        Regex.CompileToAssembly(rciList, an);
   }
}
Imports System.Reflection
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
        ' Match two or more occurrences of the same character.
        Dim pattern As String = "(\w)\1+"
        
        ' Use case-insensitive matching. 
        Dim rci As New RegexCompilationInfo(pattern, RegexOptions.IgnoreCase,
                                            "DuplicateChars", "CustomRegexes", 
                                            True, TimeSpan.FromSeconds(2))

        ' Define an assembly to contain the compiled regular expression.
        Dim an As New AssemblyName()
        an.Name = "RegexLib"
        Dim rciList As RegexCompilationInfo() = New RegexCompilationInfo() { rci }

        ' Compile the regular expression and create the assembly.
        Regex.CompileToAssembly(rciList, an)
   End Sub
End Module

Wzorzec (\w)\1+ wyrażenia regularnego jest zdefiniowany, jak pokazano w poniższej tabeli.

Wzorce Opis
(\w) Dopasuj dowolny znak słowa i przypisz go do pierwszej grupy przechwytywania.
\1+ Dopasuj co najmniej jedno wystąpienie wartości pierwszej przechwyconej grupy.

W poniższym przykładzie użyto wyrażenia regularnego DuplicatedChars do identyfikowania zduplikowanych znaków w tablicy ciągów. Po wywołaniu konstruktora DuplicatedChars zmienia interwał limitu czasu na 5 sekund.

using CustomRegexes;
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      var rgx = new DuplicateChars(TimeSpan.FromSeconds(.5));
      
      string[] values = { "Greeeeeat", "seed", "deed", "beam", 
                          "loop", "Aardvark" };
      // Display regex information.
      Console.WriteLine("Regular Expression Pattern: {0}", rgx);
      Console.WriteLine("Regex timeout value: {0} seconds\n", 
                        rgx.MatchTimeout.TotalSeconds);
      
      // Display matching information.
      foreach (var value in values) {
         Match m = rgx.Match(value);
         if (m.Success)
            Console.WriteLine("'{0}' found in '{1}' at positions {2}-{3}",
                              m.Value, value, m.Index, m.Index + m.Length - 1);
         else
            Console.WriteLine("No match found in '{0}'", value);
      }                                                         
   }
}
// The example displays the following output:
//       Regular Expression Pattern: (\w)\1+
//       Regex timeout value: 0.5 seconds
//       
//       //eeeee// found in //Greeeeeat// at positions 2-6
//       //ee// found in //seed// at positions 1-2
//       //ee// found in //deed// at positions 1-2
//       No match found in //beam//
//       //oo// found in //loop// at positions 1-2
//       //Aa// found in //Aardvark// at positions 0-1
Imports CustomRegexes
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim rgx As New DuplicateChars(TimeSpan.FromSeconds(.5))
      
      Dim values() As String = { "Greeeeeat", "seed", "deed", "beam", 
                                 "loop", "Aardvark" }
      ' Display regex information.
      Console.WriteLine("Regular Expression Pattern: {0}", rgx)
      Console.WriteLine("Regex timeout value: {0} seconds", 
                        rgx.MatchTimeout.TotalSeconds)
      Console.WriteLine()
      
      ' Display matching information.
      For Each value In values
         Dim m As Match = rgx.Match(value)
         If m.Success Then
            Console.WriteLine("'{0}' found in '{1}' at positions {2}-{3}",
                              m.Value, value, m.Index, m.Index + m.Length - 1)
         Else
            Console.WriteLine("No match found in '{0}'", value)
         End If   
      Next                                                         
   End Sub
End Module
' The example displays the following output:
'       Regular Expression Pattern: (\w)\1+
'       Regex timeout value: 0.5 seconds
'       
'       'eeeee' found in 'Greeeeeat' at positions 2-6
'       'ee' found in 'seed' at positions 1-2
'       'ee' found in 'deed' at positions 1-2
'       No match found in 'beam'
'       'oo' found in 'loop' at positions 1-2
'       'Aa' found in 'Aardvark' at positions 0-1

Uwagi

Właściwość MatchTimeout definiuje domyślny interwał limitu czasu dla skompilowanego wyrażenia regularnego. Ta wartość reprezentuje przybliżoną ilość czasu, przez jaką skompilowane wyrażenie regularne wykona pojedynczą operację dopasowania przed przekroczeniem limitu czasu operacji, a aparat wyrażeń regularnych zgłasza RegexMatchTimeoutException wyjątek podczas następnego sprawdzania chronometrażu.

Ważne

Zalecamy, aby zawsze ustawiać domyślną wartość limitu czasu dla skompilowanego wyrażenia regularnego. Użytkownicy biblioteki wyrażeń regularnych mogą zastąpić wartość limitu czasu, przekazując TimeSpan wartość reprezentującą nowy interwał limitu czasu do konstruktora klasy skompilowanego wyrażenia regularnego.

Domyślną wartość limitu czasu można przypisać do RegexCompilationInfo obiektu na dowolny z następujących sposobów:

Aby ustawić rozsądny interwał limitu czasu, rozważ następujące czynniki:

  • Długość i złożoność wzorca wyrażenia regularnego. Dłuższe i bardziej złożone wyrażenia regularne wymagają więcej czasu niż krótsze i prostsze.

  • Oczekiwane obciążenie maszyny. Przetwarzanie zajmuje więcej czasu w systemach z wysokim użyciem procesora CPU i pamięci.

Dotyczy

Zobacz też