If options are specified inline, a minus sign ( -) before an option or set of options turns off those options. ' The example displays the following output: RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace įoreach (Match match in Regex.Matches(input, pattern, options))Ĭonsole.WriteLine("'.", match.Value, match.Index)
String input = "Dogs are decidedly good pets." It uses the options parameter of the Regex.Match(String, String, RegexOptions) method to enable case-insensitive matching and to ignore pattern white space when identifying words that begin with the letter "d". The following example provides an illustration. However, the property does not reflect inline options in the regular expression pattern itself. When options are supplied to a Regex instance by using the options parameter of a class constructor, the options are assigned to the property. The options parameter is a bitwise OR combination of enumerated values. In the options parameter of a class constructor or static ( Shared in Visual Basic) pattern-matching method, such as Regex(String, RegexOptions) or Regex.Match(String, String, RegexOptions). You can specify options for regular expressions in one of three ways: For more information, see Comparison Using the Invariant Culture. For more information, see ECMAScript Matching Behavior. For more information, see Right-to-Left Mode.Įnable ECMAScript-compliant behavior for the expression. Search moves from right to left instead of from left to right. For more information, see Ignore White Space.Ĭhange the search direction. For more information, see Compiled Regular Expressions.Įxclude unescaped white space from the pattern, and enable comments after a number sign ( #). For more information, see Explicit Captures Only.Ĭompile the regular expression to an assembly. The only valid captures are explicitly named or numbered groups of the form (? subexpression ). For more information, see Single-line Mode.ĭo not capture unnamed groups. Use single-line mode, where the period (.) matches every character (instead of every character except \n). For more information, see Multiline Mode. Use multiline mode, where ^ and $ match the beginning and end of each line (instead of the beginning and end of the input string). For more information, see Case-Insensitive Matching. For more information, see Default Options.
These options, which are listed in the following table, can be included inline as part of the regular expression pattern, or they can be supplied to a class constructor or static pattern matching method as a enumeration value. You can modify these and several other aspects of default regular expression behavior by specifying regular expression options. By default, the comparison of an input string with any literal characters in a regular expression pattern is case sensitive, white space in a regular expression pattern is interpreted as literal white-space characters, and capturing groups in a regular expression are named implicitly as well as explicitly.