RegexOptions Enum

Definition

Provides enumerated values to use to set regular expression options.

This enumeration supports a bitwise combination of its member values.

[System.Flags]
public enum RegexOptions
Inheritance
RegexOptions
Attributes

Fields

Name Value Description
None 0

Specifies that no options are set. For more information about the default behavior of the regular expression engine, see the "Default Options" section in the Regular Expression Options article.

IgnoreCase 1

Specifies case-insensitive matching. For more information, see the "Case-Insensitive Matching " section in the Regular Expression Options article.

Multiline 2

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. For more information, see the "Multiline Mode" section in the Regular Expression Options article.

ExplicitCapture 4

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 (?:...). For more information, see the "Explicit Captures Only" section in the Regular Expression Options article.

Compiled 8

Specifies that the regular expression is compiled to MSIL code, instead of being interpreted. Compiled regular expressions maximize run-time performance at the expense of initialization time. This value should not be assigned to the Options property when calling the CompileToAssembly(RegexCompilationInfo[], AssemblyName) method. For more information, see the "Compiled Regular Expressions" section in the Regular Expression Options article.

Singleline 16

Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n). For more information, see the "Single-line Mode" section in the Regular Expression Options article.

IgnorePatternWhitespace 32

Eliminates unescaped white space from the pattern and enables comments marked with #. However, this value does not affect or eliminate white space in character classes, numeric quantifiers, or tokens that mark the beginning of individual regular expression language elements. For more information, see the "Ignore White Space" section of the Regular Expression Options article.

RightToLeft 64

Specifies that the search will be from right to left instead of from left to right. For more information, see the "Right-to-Left Mode" section in the Regular Expression Options article.

ECMAScript 256

Enables ECMAScript-compliant behavior for the expression. This value can be used only in conjunction with the IgnoreCase, Multiline, and Compiled values. The use of this value with any other values results in an exception.

For more information on the ECMAScript option, see the "ECMAScript Matching Behavior" section in the Regular Expression Options article.

CultureInvariant 512

Specifies that cultural differences in language is ignored. For more information, see the "Comparison Using the Invariant Culture" section in the Regular Expression Options article.

NonBacktracking 1024

Enable matching using an approach that avoids backtracking and guarantees linear-time processing in the length of the input.

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.

using System;
using System.Text.RegularExpressions;

public class Test
{
    public static void Main ()
    {
        // Define a case-insensitive regular expression for repeated words.
        Regex rxInsensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);
        // Define a case-sensitive regular expression for repeated words.
        Regex rxSensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled);

        // Define a test string.        
        string text = "The the quick brown fox  fox jumps over the lazy dog dog.";
        
        // Find matches using case-insensitive regular expression.
        MatchCollection matches = rxInsensitive.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found in:\n   {1}", 
                          matches.Count, 
                          text);

        // Report on each match.
        foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                              groups["word"].Value, 
                              groups[0].Index, 
                              groups[1].Index);
        }
        Console.WriteLine();
        
        // Find matches using case-sensitive regular expression.
        matches = rxSensitive.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found in:\n   {1}", 
                          matches.Count, 
                          text);

        // Report on each match.
        foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                              groups["word"].Value, 
                              groups[0].Index, 
                              groups[1].Index);
        }
    }
}
// The example produces the following output to the console:
//       3 matches found in:
//          The the quick brown fox  fox jumps 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 jumps over the lazy dog dog.
//       'fox' repeated at positions 20 and 25
//       'dog' repeated at positions 50 and 54

Remarks

A RegexOptions value can be provided as a parameter to the following members of the Regex class:

A RegexOptions value can also be supplied as a parameter to the RegexCompilationInfo constructor, or it can be assigned directly to the RegexCompilationInfo.Options property. The resulting RegexCompilationInfo object is then used in the call to the Regex.CompileToAssembly method.

Several options provided by members of the RegexOptions enumeration (in particular, by its ExplicitCapture, IgnoreCase, Multiline, and Singleline members) can instead be provided by using an inline option character in the regular expression pattern. For details, see Regular Expression Options.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

See also