Regex.MatchTimeout Property

.NET Framework (current version)
 

Gets the time-out interval of the current instance.

Namespace:   System.Text.RegularExpressions
Assembly:  System (in System.dll)

public TimeSpan MatchTimeout { get; }

Property Value

Type: System.TimeSpan

The maximum time interval that can elapse in a pattern-matching operation before a RegexMatchTimeoutException is thrown, or Regex.InfiniteMatchTimeout if time-outs are disabled.

The MatchTimeout property defines the approximate maximum time interval for a Regex instance to execute a single matching operation before the operation times out. The regular expression engine throws a RegexMatchTimeoutException exception during its next timing check after the time-out interval has elapsed. This prevents the regular expression engine from processing input strings that require excessive backtracking. For more information, see Backtracking in Regular Expressions and Best Practices for Regular Expressions in the .NET Framework.

This property is read-only. You can set its value explicitly for an individual Regex object by calling the Regex.Regex(String, RegexOptions, TimeSpan) constructor; and you can set its value for all Regex matching operations in an application domain by calling the AppDomain.SetData method and providing a TimeSpan value for the "REGEX_DEFAULT_MATCH_TIMEOUT" property, as the following example illustrates.

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

If you do not explicitly set a time-out interval, the default value Regex.InfiniteMatchTimeout is used, and matching operations do not time out.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: