Regex.MatchTimeout Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the time-out interval of the current instance.
public:
property TimeSpan MatchTimeout { TimeSpan get(); };
public TimeSpan MatchTimeout { get; }
member this.MatchTimeout : TimeSpan
Public ReadOnly Property MatchTimeout As TimeSpan
The maximum time interval that can elapse in a pattern-matching operation before a RegexMatchTimeoutException is thrown, or 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 and Best Practices for Regular Expressions.
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
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim domain As AppDomain = AppDomain.CurrentDomain
' Set a timeout interval of 2 seconds.
domain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(2))
Dim timeout As Object = domain.GetData("REGEX_DEFAULT_MATCH_TIMEOUT")
Console.WriteLine("Default regex match timeout: {0}",
If(timeout Is Nothing, "<null>", timeout))
Dim rgx As New Regex("[aeiouy]")
Console.WriteLine("Regular expression pattern: {0}", rgx.ToString())
Console.WriteLine("Timeout interval for this regex: {0} seconds",
rgx.MatchTimeout.TotalSeconds)
End Sub
End Module
' 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.
Product | Versions |
---|---|
.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 |
.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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: