Compartir a través de


Regular Expression Options

You can modify a regular expression pattern with options that affect matching behavior. Regular expression options can be set in one of two basic ways: they can be specified in the options parameter in the Regex(pattern, options) constructor, where options is a bitwise OR combination of RegexOptions enumerated values, or they can be set within the regular expression pattern using the inline (?imnsx-imnsx:) grouping construct or (?imnsx-imnsx) miscellaneous construct.

In inline option constructs, a minus sign (-) before an option or set of options turns off those options. For example, the inline construct (?ix-ms) turns on the IgnoreCase and IgnorePatternWhiteSpace options and turns off the Multiline and Singleline options. All regular expression options are turned off by default.

The following table lists the members of the RegexOptions enumeration and the equivalent inline option characters. Note that the options RightToLeft and Compiled apply only to an expression as a whole and are not allowed inline. (They can be specified only in the options parameter to the Regex constructor.) The options None and ECMAScript are not allowed inline.

RegexOption member Inline character Description
None N/A Specifies that no options are set.
IgnoreCase i Specifies case-insensitive matching.
Multiline m Specifies multiline mode. Changes the meaning of ^ and $ so that they match at the beginning and end, respectively, of any line, not just the beginning and end of the whole string.
ExplicitCapture n Specifies that the only valid captures are explicitly named or numbered groups of the form (?<name>). This allows parentheses to act as noncapturing groups without the syntactic clumsiness of (?:).
Compiled N/A Specifies that the regular expression will be compiled to an assembly. Generates Microsoft intermediate language (MSIL) code for the regular expression; yields faster execution at the expense of startup time.
Singleline s Specifies single-line mode. Changes the meaning of the period character (.) so that it matches every character (instead of every character except \n).
IgnorePatternWhitespace x Specifies that unescaped white space is excluded from the pattern and enables comments following a number sign (#). (For a list of escaped white-space characters, see Character Escapes.) Note that white space is never eliminated from within a character class.
RightToLeft N/A Specifies that the search moves from right to left instead of from left to right. A regular expression with this option moves to the left of the starting position instead of to the right. (Therefore, the starting position should be specified as the end of the string instead of the beginning.) This option cannot be specified in midstream, to prevent the possibility of crafting regular expressions with infinite loops. However, the (?<) lookbehind constructs provide something similar that can be used as a subexpression.

RightToLeft changes the search direction only. It does not reverse the substring that is searched for. The lookahead and lookbehind assertions do not change: lookahead looks to the right; lookbehind looks to the left.

ECMAScript N/A Specifies that ECMAScript-compliant behavior is enabled for the expression. This option can be used only in conjunction with the IgnoreCase and Multiline flags. Use of ECMAScript with any other flags results in an exception.
CultureInvariant N/A Specifies that cultural differences in language is ignored. See Performing Culture-Insensitive Operations in the RegularExpressions Namespace for more information.

See Also

Regular Expression Language Elements