AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(2));
isMatch = IsMatch(input, pattern);
Console.WriteLine();
isMatch = IsMatch(input, pattern, TimeSpan.FromSeconds(4));
}
static Boolean IsMatch(string input, string pattern)
{
try
{
return Regex.IsMatch(input, pattern, RegexOptions.None);
}
catch (RegexMatchTimeoutException ex)
{
// handle exception
Console.WriteLine("Match timed out!");
Console.WriteLine("- Timeout interval specified: " + ex.MatchTimeout);
Console.WriteLine("- Pattern: " + ex.Pattern);
Console.WriteLine("- Input: " + ex.Input);
}
return false;
}
static Boolean IsMatch(string input, string pattern, TimeSpan timeout)
{
try
{
return Regex.IsMatch(input, pattern, RegexOptions.None, timeout);
}
catch(ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
catch (RegexMatchTimeoutException ex)
{
// handle exception
Console.WriteLine("Match timed out!");
Console.WriteLine("- Timeout interval specified: " + ex.MatchTimeout);
Console.WriteLine("- Pattern: " + ex.Pattern);
Console.WriteLine("- Input: " + ex.Input);
}
return false;
}
}</pre>