RegexCompilationInfo.MatchTimeout 屬性

定義

取得或設定規則運算式的預設逾時間隔。

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

屬性值

RegexMatchTimeoutException擲回之前,可在模式比對作業中流逝的預設最大時間間隔,或者為InfiniteMatchTimeout(如果停用逾時)。

範例

下列範例會定義名為 DuplicateChars 的單一編譯正則運算式,可識別輸入字串中相同字元的兩個或多個出現專案。 編譯的正則運算式預設逾時為 2 秒。 當您執行範例時,它會建立名為 RegexLib.dll 的類別庫,其中包含編譯的正則運算式。

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

規則運算式模式 (\w)\1+ 的定義如下表所示。

模式 描述
(\w) 比對任何字字元,並將它指派給第一個擷取群組。
\1+ 比對第一個擷取群組值的一或多個出現次數。

下列範例會 DuplicatedChars 使用正則運算式來識別字串陣列中的重複字元。 呼叫建構函式時 DuplicatedChars ,它會將逾時間隔變更為 .5 秒。

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

備註

屬性 MatchTimeout 會定義已編譯正則運算式的預設逾時間隔。 這個值代表編譯正則運算式會在作業逾時之前執行單一比對作業的大約時間量,而正則運算式引擎會在下次檢查期間擲回 RegexMatchTimeoutException 例外狀況。

重要

建議您一律為編譯的正則運算式設定預設逾時值。 正則運算式程式庫的取用者可以將代表新逾時間隔的值傳遞 TimeSpan 至編譯正則運算式的類別建構函式,以覆寫該逾時值。

您可以使用下列任何一種方式,將預設逾時值指派給 RegexCompilationInfo 物件:

若要設定合理的逾時間隔,請考慮下列因素:

  • 正則運算式模式的長度和複雜度。 較長且更複雜的正則運算式需要比較短且更簡單的時間多。

  • 預期的機器負載。 在高 CPU 和記憶體使用率的系統上,處理需要更多時間。

適用於

另請參閱