Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Regex Constructor (String)

Initializes a new instance of the Regex class for the specified regular expression.

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

public Regex(
	string pattern


Type: System.String

The regular expression pattern to match.


A regular expression parsing error occurred.


pattern is null.

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.

Calling the Regex(String) constructor is equivalent to calling the Regex(String, RegexOptions) constructor with a value of None for the options argument.

A Regex object is immutable, which means that it can be used only for the match pattern you define when you create it. However, it can be used any number of times without being recompiled.

This constructor instantiates a regular expression object that attempts a case-sensitive match of any alphabetical characters defined in pattern. For a case-insensitive match, use the Regex.Regex(String, RegexOptions) constructor.

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+";
      string text = "The threaded application ate up the thread pool as it executed.";
      MatchCollection matches;

      Regex defaultRegex = new Regex(pattern);
      // Get matches of pattern in text
      matches = defaultRegex.Matches(text);
      Console.WriteLine("Parsing '{0}'", 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.' 
//       0. threaded 
//       1. application 
//       2. ate 
//       3. the 
//       4. thread 
//       5. as

Note that the regular expression pattern cannot match the word "The" at the beginning of the text, because comparisons are case-sensitive by default. For an example of case-insensitive comparison, see the Regex(String, RegexOptions) constructor.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
© 2015 Microsoft