Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
RegexCompilationInfo Constructor (String, RegexOptions, String, String, Boolean, TimeSpan)

RegexCompilationInfo Constructor (String, RegexOptions, String, String, Boolean, TimeSpan)

.NET Framework 4.6 and 4.5

Initializes a new instance of the RegexCompilationInfo class that contains information about a regular expression with a specified time-out value to be included in an assembly.

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

'Declaration
Public Sub New ( _
	pattern As String, _
	options As RegexOptions, _
	name As String, _
	fullnamespace As String, _
	ispublic As Boolean, _
	matchTimeout As TimeSpan _
)

Parameters

pattern
Type: System.String

The regular expression to compile.

options
Type: System.Text.RegularExpressions.RegexOptions

The regular expression options to use when compiling the regular expression.

name
Type: System.String

The name of the type that represents the compiled regular expression.

fullnamespace
Type: System.String

The namespace to which the new type belongs.

ispublic
Type: System.Boolean

true to make the compiled regular expression publicly visible; otherwise, false.

matchTimeout
Type: System.TimeSpan

The default time-out interval for the regular expression.

ExceptionCondition
ArgumentException

name is String.Empty.

ArgumentNullException

pattern is Nothing.

-or-

name is Nothing.

-or-

fullnamespace is Nothing.

ArgumentOutOfRangeException

matchTimeout is negative, zero, or greater than approximately 24 days.

Each parameter of the RegexCompilationInfo(String, RegexOptions, String, String, Boolean) constructor directly corresponds to a property of the RegexCompilationInfo class. Because all properties are read/write, their values can also be assigned directly.

The CompileToAssembly method generates an assembly that contains compiled regular expressions. Therefore, you should not specify Compiled as one of the values of options.

If ispublic is true, the compiled regular expression class is given public accessibility. That is, it can be instantiated from code that executes in any assembly. If ispublic is false, the compiled regular expression class is given internal (in C#) or Friend (in Visual Basic) accessibility. That is, it can be instantiated only from code that executes in the same assembly as the regular expression class.

The matchTimeout parameter defines the default time-out interval for the compiled regular expression. This value represents the approximate amount of time that a compiled regular expression object 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. For additional information about the time-out value, see the MatchTimeout property.

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 this constructor overload.

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.

Imports System.Reflection
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
        ' Match two or more occurrences of the same character. 
        Dim pattern As String = "(\w)\1+" 

        ' Use case-insensitive matching.  
        Dim rci As New RegexCompilationInfo(pattern, RegexOptions.IgnoreCase,
                                            "DuplicateChars", "CustomRegexes", 
                                            True, TimeSpan.FromSeconds(2))

        ' Define an assembly to contain the compiled regular expression. 
        Dim an As New AssemblyName()
        an.Name = "RegexLib" 
        Dim rciList As RegexCompilationInfo() = New RegexCompilationInfo() { rci }

        ' Compile the regular expression and create the assembly.
        Regex.CompileToAssembly(rciList, an)
   End Sub 
End Module

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.

Imports CustomRegexes
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim rgx As New DuplicateChars(TimeSpan.FromSeconds(.5))

      Dim values() As String = { "Greeeeeat", "seed", "deed", "beam", 
                                 "loop", "Aardvark" }
      ' Display regex information.
      Console.WriteLine("Regular Expression Pattern: {0}", rgx)
      Console.WriteLine("Regex timeout value: {0} seconds", 
                        rgx.MatchTimeout.TotalSeconds)
      Console.WriteLine()

      ' Display matching information. 
      For Each value In values
         Dim m As Match = rgx.Match(value)
         If m.Success Then
            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)
         End If    
      Next                                                          
   End Sub 
End Module 
' 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.6, 4.5
Show:
© 2015 Microsoft