title: How do I timeout Regex operations to prevent hanging in .NET 4.5?
body: <p>There are times when being able to limit the pattern matching duration of regex operations could be useful. In particular, when working with user supplied patterns to match data, the pattern might exhibit poor performance due to nested quantifiers and excessive back-tracking (see <a href="http://www.regular-expressions.info/catastrophic.html" rel="nofollow noreferrer">catastrophic backtracking</a>). One way to apply a timeout is to run the regex asynchronously, but this can be tedious and clutters the code.</p>
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx" rel="nofollow noreferrer">what's new in the .NET Framework 4.5 Developer Preview</a> it looks like there's a new built-in approach to support this:</p>
  <blockquote>
  <p>Ability to limit how long the regular expression engine will attempt
  to resolve a regular expression before it times out.</p>
  </blockquote>
  <p>How can I use this feature? Also, what do I need to be aware of when using it?</p>
  <p><sub>Note: I'm <a href="https://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/">asking <em>and</em> answering</a> this question since <a href="https://blog.stackoverflow.com/2012/05/encyclopedia-stack-exchange/">it's encouraged</a>.</sub></p>