.NET 4.5 gives us improved control on the resolution of regular expressions so we can react when they don't resolve on time. This is extremely useful if we don't control the regular expressions/patterns, such as the ones provided by the users.
A badly formed pattern can have bad performance due to excessive backtracking and this new feature is really a lifesaver.
Next we are going to control the timeout in the regular expression, where we will react if the operation takes more than 1 millisecond:
Create a new Visual Studio project of type Console Application, named
caRegexTimeout
.Open the Program.cs file and add a
using
clause for using regular expressions:Using System.Text.RegularExpressions;
Add the following method and call it from the
Main
function:private static void ExecuteRegexExpression() { bool RegExIsMatch = false; string testString = "One Tile to rule them all, One Tile to find them… "; string RegExPattern = @"([a-z ]...