This documentation is archived and is not being maintained.

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


Type: System.String

The regular expression pattern to match.

Type: System.Text.RegularExpressions.RegexOptions

A bitwise OR combination of the enumeration values.


A regular expression parsing error has occurred.


pattern is null.


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 - Quick Reference 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 example illustrates how to use this constructor to instantiate a regular expression that matches any word that begins with the letters "a" or "t".

string pattern = @"\b[at]\w+";
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled;
string text = "The threaded application ate up the thread pool as it executed.";
MatchCollection matches;

Regex optionRegex = new Regex(pattern, options);
Console.WriteLine("Parsing '{0}' with options {1}:", text, options.ToString());
// Get matches of pattern in text
matches = optionRegex.Matches(text);
// Iterate matches 
for (int ctr = 1; ctr <= matches.Count; ctr++)
   Console.WriteLine("{0}. {1}", ctr, matches[ctr-1].Value);

Note that the match collection includes the word "The" that begins the text because the options parameter has defined case-insensitive comparisons.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0