Can you explain the usefulness of new property Regex.MatchTimeout introduced by .NET Framework 4.5?

There are times when being able to limit the pattern matching duration of Regex operations could be useful, especially when working with user supplied patterns to match data.
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. 
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 value for the 
“REGEX_DEFAULT_MATCH_TIMEOUT” property. If you do not explicitly set a timeout interval, the default value Regex.InfiniteMatchTimeout is used, and matching operations do not time out.

string input = "The quick brown fox jumps over the lazy dog.";
string pattern = @"([a-z ]+)*!";
try
{
bool result = Regex.IsMatch(input, pattern, RegexOptions.None, TimeSpan.FromSeconds(4));
}
catch (RegexMatchTimeoutException ex)
{
Console.WriteLine("Match timed out!");
Console.WriteLine("- Timeout interval specified: " + ex.MatchTimeout);
Console.WriteLine("- Pattern: " + ex.Pattern);
Console.WriteLine("- Input: " + ex.Input);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*