Controlling the timeout in regular expressions

.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.

How to do it...

Next we are going to control the timeout in the regular expression, where we will react if the operation takes more than 1 millisecond:

  1. Create a new Visual Studio project of type Console Application, named caRegexTimeout.
  2. Open the Program.cs file and add a using clause for using regular expressions:
    Using System.Text.RegularExpressions; ...

Get Microsoft .NET Framework 4.5 Quickstart Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.