Export (0) Print
Expand All

RegexCompilationInfo.MatchTimeout Property

.NET Framework 4.5

Gets or sets the regular expression's default time-out interval.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)

public TimeSpan MatchTimeout { get; set; }

Property Value

Type: System.TimeSpan
The default maximum time interval that can elapse in a pattern-matching operation before a RegexMatchTimeoutException is thrown, or Regex.InfiniteMatchTimeout if time-outs are disabled.

The MatchTimeout property defines the default time-out interval for the compiled regular expression. This value represents the approximate amount of time that a compiled regular expression will execute a single matching operation before the operation times out and the regular expression engine throws a RegexMatchTimeoutException exception during its next timing check.

Important noteImportant

We recommend that you always set a default time-out value for a compiled regular expression. Consumers of your regular expression library can override that time-out value by passing a TimeSpan value that represents the new time-out interval to the compiled regular expression's class constructor.

You can assign a default time-out value to a RegexCompilationInfo object in any of the following ways:

To set a reasonable time-out interval, consider the following factors:

  • The length and complexity of the regular expression pattern. Longer and more complex regular expressions require more time than shorter and simpler ones.

  • The expected machine load. Processing takes more time on systems with high CPU and memory utilization.

The following example defines a single compiled regular expression named DuplicateChars that identifies two or more occurrences of the same character in an input string. The compiled regular expression has a default time-out of 2 seconds. When you execute the example, it creates a class library named RegexLib.dll that contains the compiled regular expression.

using System;
using System.Reflection;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
        // Match two or more occurrences of the same character. 
        string pattern = @"(\w)\1+";

        // Use case-insensitive matching.  
        var rci = new RegexCompilationInfo(pattern, RegexOptions.IgnoreCase,
                                           "DuplicateChars", "CustomRegexes", 
                                           true, TimeSpan.FromSeconds(2));

        // Define an assembly to contain the compiled regular expression. 
        var an = new AssemblyName();
        an.Name = "RegexLib";
        RegexCompilationInfo[] rciList = { rci };

        // Compile the regular expression and create the assembly.
        Regex.CompileToAssembly(rciList, an);
   }
}

The regular expression pattern (\w)\1+ is defined as shown in the following table.

Pattern

Description

(\w)

Match any word character and assign it to the first capturing group.

\1+

Match one or more occurrences of the value of the first captured group.

The following example uses the DuplicatedChars regular expression to identify duplicate characters in a string array. When it calls the DuplicatedChars constructor, it changes the time-out interval to .5 seconds.

using CustomRegexes;
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      var rgx = new DuplicateChars(TimeSpan.FromSeconds(.5));

      string[] values = { "Greeeeeat", "seed", "deed", "beam", 
                          "loop", "Aardvark" };
      // Display regex information.
      Console.WriteLine("Regular Expression Pattern: {0}", rgx);
      Console.WriteLine("Regex timeout value: {0} seconds\n", 
                        rgx.MatchTimeout.TotalSeconds);

      // Display matching information. 
      foreach (var value in values) {
         Match m = rgx.Match(value);
         if (m.Success)
            Console.WriteLine("'{0}' found in '{1}' at positions {2}-{3}",
                              m.Value, value, m.Index, m.Index + m.Length - 1);
         else
            Console.WriteLine("No match found in '{0}'", value);
      }                                                         
   }
}
// The example displays the following output: 
//       Regular Expression Pattern: (\w)\1+ 
//       Regex timeout value: 0.5 seconds 
//        
//       //eeeee// found in //Greeeeeat// at positions 2-6 
//       //ee// found in //seed// at positions 1-2 
//       //ee// found in //deed// at positions 1-2 
//       No match found in //beam// 
//       //oo// found in //loop// at positions 1-2 
//       //Aa// found in //Aardvark// at positions 0-1

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft