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

プロパティ値

TimeSpan

RegexMatchTimeoutException がスローされる前にパターン一致操作で経過できる既定の最大時間間隔、またはタイムアウトが無効な場合は InfiniteMatchTimeout

次の例では、入力文字列内で同じ文字が 2 つ以上出現する場合を識別する、 という名前の単一のコンパイル済み正規表現 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+ 最初にキャプチャされたグループの値の 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 は、コンパイルされた正規表現の既定のタイム アウト間隔を定義します。 この値は、コンパイルされた正規表現が操作が終了する前に 1 つの一致操作を実行し、次のタイミング チェック中に正規表現エンジンが例外をスローするおおよその時間を表 RegexMatchTimeoutException します。

重要

コンパイル済みの正規表現には、常に既定のタイム アウト値を設定することをお勧めします。 正規表現ライブラリのコンシューマーは、コンパイルされた正規表現のクラス コンストラクターに新しいタイム アウト間隔を表す値を渡して、そのタイム アウト値を TimeSpan オーバーライドできます。

オブジェクトに既定のタイム アウト値を割り当 RegexCompilationInfo てるには、次の方法があります。

妥当なタイム アウト間隔を設定するには、次の要因を考慮してください。

  • 正規表現パターンの長さと複雑さ。 正規表現が長く複雑な場合は、より短く単純な正規表現よりも時間が必要です。

  • 予想されるマシンの負荷。 CPU 使用率とメモリ使用率が高いシステムでは、処理に時間がかかる。

適用対象

こちらもご覧ください