Regex Constructor (String, RegexOptions)

Initializes and compiles a new instance of the Regex class for the specified regular expression, with options that modify the pattern.

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

public Regex (
	string pattern,
	RegexOptions options
public Regex (
	String pattern, 
	RegexOptions options
public function Regex (
	pattern : String, 
	options : RegexOptions



The regular expression pattern to match.


A bitwise OR combination of RegexOptions enumeration values.

Exception typeCondition


Regular expression parsing error.


pattern is a null reference (Nothing in Visual Basic).


options contains an invalid flag.

The pattern parameter consists of various regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see the .NET Framework Regular Expressions and Regular Expression Language Elements topics.

A Regex object is immutable, which means that it can be used only for the match parameters defined when it is created. It can be used any number of times without being recompiled, however.

The following code example illustrates the use of this constructor to instantiate a regular expression with a specified pattern and options.

// Define a regular expression for repeated words.
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
  RegexOptions.Compiled | RegexOptions.IgnoreCase);

// Define a regular expression for repeated words.
Regex rx = new Regex("\\b(?<word>\\w+)\\s+(\\k<word>)\\b", 
    RegexOptions.Compiled | RegexOptions.IgnoreCase);

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions