Regex Constructor (String, RegexOptions)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Initializes 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 combination of the enumeration values that modify the regular expression.

Exception Condition

A regular expression parsing error occurred.


pattern is null.


options contains an invalid flag.

The pattern parameter consists of 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 you define when you create it. However, it can be used any number of times without being recompiled.

Notes to Callers:

This constructor creates a Regex object that uses the default time-out value of the application domain in which it is created. If a time-out value has not been defined for the application domain, the Regex object uses the value InfiniteMatchTimeout, which prevents the operation from timing out. The recommended constructor for creating a Regex object is Regex.Regex(String, RegexOptions, TimeSpan), which lets you set the time-out interval.

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".

using System;
using System.Text.RegularExpressions;

public class Example
   public static void Main()
      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 = 0; ctr < matches.Count; ctr++)
         Console.WriteLine("{0}. {1}", ctr, matches[ctr].Value);
// The example displays the following output:
//    Parsing 'The threaded application ate up the thread pool as it executed.'
//        with options IgnoreCase, Compiled:
//    0. The
//    1. threaded
//    2. application
//    3. ate
//    4. the
//    5. thread
//    6. as

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top