RegexOptions Enumeration
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Provides enumerated values to use to set regular expression options.
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.
Namespace: System.Text.RegularExpressions
Assembly: System (in System.dll)
Syntax
'Declaration
<FlagsAttribute> _
Public Enumeration RegexOptions
[FlagsAttribute]
public enum RegexOptions
Members
Member name | Description | |
---|---|---|
None | Specifies that no options are set. | |
IgnoreCase | Specifies case-insensitive matching. | |
Multiline | Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string. | |
ExplicitCapture | Specifies that the only valid captures are explicitly named or numbered groups of the form (?<name>…). This allows unnamed parentheses to act as noncapturing groups without the syntactic clumsiness of the expression (?:…). | |
Singleline | Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n). | |
IgnorePatternWhitespace | Eliminates unescaped white space from the pattern and enables comments marked with #. However, the RegexOptions.IgnorePatternWhitespace value does not affect or eliminate white space in character classes | |
RightToLeft | Specifies that the search will be from right to left instead of from left to right. | |
ECMAScript | Enables ECMAScript-compliant behavior for the expression. This value can be used only in conjunction with the IgnoreCase and Multiline values. The use of this value with any other values results in an exception. | |
CultureInvariant | Specifies that cultural differences in language are ignored. Ordinarily, the regular expression engine performs string comparisons based on the conventions of the current culture. If the CultureInvariant option is specified, it uses the conventions of the invariant culture. | |
Compiled | Specifies that the regular expression is compiled to an assembly. This yields faster execution but increases startup time. This value should not be assigned to the Options property when calling the CompileToAssembly method. Present only in Silverlight for Windows Phone, but not supported. |
Remarks
A RegexOptions value can be provided as a parameter to the following members of the Regex class:
The Regex.Regex(String, RegexOptions) class constructor.
The Regex.Split(String, String, RegexOptions) method.
The Regex.IsMatch(String, String, RegexOptions) method.
The Regex.Match(String, String, RegexOptions) method.
The Regex.Matches(String, String, RegexOptions) method.
The Regex.Replace(String, String, String, RegexOptions) and Regex.Replace(String, String, MatchEvaluator, RegexOptions) methods.
Several options provided by members of the RegexOptions enumeration can be specified instead by using an inline option character in the regular expression pattern. A group of inline regular expression options are enabled if they are preceded by a plus sign (+). They are disabled if they are preceded by a minussign (-). The following table lists the RegexOptions members that have inline equivalents. The None, RightToLeft, ECMAScript, and CultureInvariant members do not have inline equivalents.
RegexOptions member |
Inline character |
---|---|
IgnoreCase |
i |
Multiline |
m |
ExplicitCapture |
n |
Singleline |
s |
IgnorePatternWhitespace |
x |
Examples
The following example defines two regular expressions that identify repeated words in text but that are instantiated using different RegexOptions values. The first regular expression is case-insensitive; case is ignored when determining whether a word is identical to the preceding word. The second regular expression is case-sensitive; a word must match the case of the preceding word exactly to be considered a duplicate.
Imports System.Text.RegularExpressions
Public Module Example
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
' Define a case-insensitive regular expression for repeated words.
Dim rxInsensitive As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _
RegexOptions.IgnoreCase)
' Define a case-sensitive regular expression for repeated words.
Dim rxSensitive As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _
RegexOptions.None)
' Define a test string.
Dim text As String = "The the quick brown fox fox jumped over the lazy dog dog."
' Find matches using case-insensitive regular expression.
Dim matches As MatchCollection = rxInsensitive.Matches(text)
' Report the number of matches found.
outputBlock.Text &= String.Format("{0} matches found in:", matches.Count) & vbCrLf
outputBlock.Text &= String.Format(" {0}", text) & vbCrLf
' Report on each match.
For Each match As Match In matches
Dim groups As GroupCollection = match.Groups
outputBlock.Text &= String.Format("'{0}' repeated at positions {1} and {2}", _
groups.Item("word").Value, _
groups.Item(0).Index, _
groups.Item(1).Index) & vbCrLf
Next
outputBlock.Text &= vbCrLf
' Find matches using case-sensitive regular expression.
matches = rxSensitive.Matches(text)
' Report the number of matches found.
outputBlock.Text &= String.Format("{0} matches found in:", matches.Count) & vbCrLf
outputBlock.Text &= String.Format(" {0}", text) & vbCrLf
' Report on each match.
For Each match As Match In matches
Dim groups As GroupCollection = match.Groups
outputBlock.Text &= String.Format("'{0}' repeated at positions {1} and {2}", _
groups.Item("word").Value, _
groups.Item(0).Index, _
groups.Item(1).Index) & vbCrLf
Next
outputBlock.Text &= vbCrLf
End Sub
End Module
' The example produces the following output:
' 3 matches found in:
' The the quick brown fox fox jumped over the lazy dog dog.
' 'The' repeated at positions 0 and 4
' 'fox' repeated at positions 20 and 25
' 'dog' repeated at positions 50 and 54
'
' 2 matches found in:
' The the quick brown fox fox jumped over the lazy dog dog.
' 'fox' repeated at positions 20 and 25
' 'dog' repeated at positions 50 and 54
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
// Define a case-insensitive regular expression for repeated words.
Regex rxInsensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.IgnoreCase);
// Define a case-sensitive regular expression for repeated words.
Regex rxSensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.None);
// Define a test string.
string text = "The the quick brown fox fox jumped over the lazy dog dog.";
// Find matches using case-insensitive regular expression.
MatchCollection matches = rxInsensitive.Matches(text);
// Report the number of matches found.
outputBlock.Text += String.Format("{0} matches found in:\n {1}",
matches.Count,
text) + "\n";
// Report on each match.
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
outputBlock.Text += String.Format("'{0}' repeated at positions {1} and {2}",
groups["word"].Value,
groups[0].Index,
groups[1].Index) + "\n";
}
outputBlock.Text += "\n";
// Find matches using case-sensitive regular expression.
matches = rxSensitive.Matches(text);
// Report the number of matches found.
outputBlock.Text += String.Format("{0} matches found in:\n {1}",
matches.Count,
text) + "\n";
// Report on each match.
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
outputBlock.Text += String.Format("'{0}' repeated at positions {1} and {2}",
groups["word"].Value,
groups[0].Index,
groups[1].Index) + "\n";
}
}
}
// The example produces the following output:
// 3 matches found in:
// The the quick brown fox fox jumped over the lazy dog dog.
// 'The' repeated at positions 0 and 4
// 'fox' repeated at positions 20 and 25
// 'dog' repeated at positions 50 and 54
//
// 2 matches found in:
// The the quick brown fox fox jumped over the lazy dog dog.
// 'fox' repeated at positions 20 and 25
// 'dog' repeated at positions 50 and 54
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.