Regex Constructor (String, RegexOptions)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Initializes and compiles a new instance of the Regex class for the specified regular expression, with options that modify the pattern.
Assembly: System (in System.dll)
Parameters
- pattern
- Type: System.String
The regular expression pattern to match.
- options
- Type: System.Text.RegularExpressions.RegexOptions
A bitwise combination of the enumeration values.
| Exception | Condition |
|---|---|
| ArgumentException | A regular expression parsing error occurred. |
| ArgumentNullException | pattern is Nothing. |
| ArgumentOutOfRangeException | 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 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.
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 options As RegexOptions = RegexOptions.IgnoreCase Dim text As String = "The threaded application ate up the thread pool as it executed." Dim matches As MatchCollection Dim optionRegex As New Regex(pattern, options) outputBlock.Text &= String.Format("Parsing '{0}' with options {1}:", text, options.ToString()) & vbCrLf ' Get matches of pattern in text matches = optionRegex.Matches(text) ' Iterate matches For ctr As Integer = 0 To matches.Count - 1 outputBlock.Text &= String.Format("{0}. {1}", ctr, matches(ctr).Value) & vbCrLf Next
Note that the match collection includes the word "The" that begins the text because the options parameter has defined case-insensitive comparisons.