Regex.MatchTimeout プロパティ

定義

現在のインスタンスのタイムアウト間隔を取得します。

C#
public TimeSpan MatchTimeout { get; }

プロパティ値

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

注釈

プロパティはMatchTimeout、インスタンスが操作がタイムアウトする前に 1 つの一致する操作を実行するためのおおよその最大時間間隔Regexを定義します。正規表現エンジンは、RegexMatchTimeoutExceptionタイムアウト間隔が経過した後チェック次のタイミングで例外をスローします。 これにより、正規表現エンジンは、過剰なバックトラッキングを必要とする入力文字列を処理できなくなります。 詳細については、「正規表現の バックトラッキングベスト プラクティス」を参照してください。

このプロパティは読み取り専用です。 コンストラクターを呼び出Regex.Regex(String, RegexOptions, TimeSpan)すことで、その値を個々Regexのオブジェクトに対して明示的に設定できます。次の例に示すように、 メソッドを呼び出AppDomain.SetDataし、"REGEX_DEFAULT_MATCH_TIMEOUT" プロパティの値をTimeSpan指定することで、アプリケーション ドメイン内のすべてのRegex一致する操作の値を設定できます。

C#
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      AppDomain domain = AppDomain.CurrentDomain;
      // Set a timeout interval of 2 seconds.
      domain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(2));
      Object timeout = domain.GetData("REGEX_DEFAULT_MATCH_TIMEOUT");
      Console.WriteLine("Default regex match timeout: {0}",
                         timeout == null ? "<null>" : timeout);

      Regex rgx = new Regex("[aeiouy]");
      Console.WriteLine("Regular expression pattern: {0}", rgx.ToString());
      Console.WriteLine("Timeout interval for this regex: {0} seconds",
                        rgx.MatchTimeout.TotalSeconds);
   }
}
// The example displays the following output:
//       Default regex match timeout: 00:00:02
//       Regular expression pattern: [aeiouy]
//       Timeout interval for this regex: 2 seconds

タイムアウト間隔を明示的に設定しない場合、既定値 Regex.InfiniteMatchTimeout が使用され、一致する操作はタイムアウトしません。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0