Regex Constructor (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Sub New ( _
    pattern As String _
)
public Regex(
    string pattern
)

Parameters

  • pattern
    Type: System.String
    The regular expression pattern to match.

Exceptions

Exception Condition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

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

Remarks

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 Regular Expressions as a Language and Regular Expression Language Elements topics in the .NET Framework documentation.

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.

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

Examples

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

Dim pattern As String = "\b[at]\w+"
Dim text As String = "The threaded application ate up the thread pool as it executed."
Dim matches As MatchCollection

Dim defaultRegex As New Regex(pattern)
' Get matches of pattern in text
matches = defaultRegex.Matches(text)
outputBlock.Text &= String.Format("Parsing '{0}'", text) & vbCrLf
' Iterate matches   
For ctr As Integer = 0 To matches.Count - 1
   outputBlock.Text &= String.Format("{0}. {1}", ctr, matches(ctr).Value) & vbCrLf
Next
' This 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      
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);
outputBlock.Text += String.Format("Parsing '{0}'", text) + "\n";
// Iterate matches   
for (int ctr = 1; ctr <= matches.Count; ctr++)
   outputBlock.Text += String.Format("{0}. {1}", ctr, matches[ctr - 1].Value) + "\n";
// This 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, because comparisons are case-sensitive by default, the regular expression pattern fails to match the word "The" at the beginning of the text.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.