C# Reference ยท Strings and Text
RegexOptions
Reference for the RegexOptions enum in C#: IgnoreCase, Multiline, Singleline, IgnorePatternWhitespace, ExplicitCapture, RightToLeft, Compiled, NonBacktracking.
RegexOptions is a flags enum that changes how a pattern is interpreted or executed. You combine flags with | and pass them to a Regex constructor or to a static method. Some options change meaning (IgnoreCase, Multiline, Singleline), some change how the pattern text is read (IgnorePatternWhitespace, ExplicitCapture), and some change the engine (Compiled, NonBacktracking).
Most options can also be switched on inside the pattern: (?i) for IgnoreCase, (?m) for Multiline, (?s) for Singleline, (?x) for IgnorePatternWhitespace, (?n) for ExplicitCapture. A leading (?i) is handy when you cannot pass options.
var re = new Regex("^error", RegexOptions.IgnoreCase | RegexOptions.Multiline);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
[Flags] public enum RegexOptions { None = 0, IgnoreCase = 1, Multiline = 2, ExplicitCapture = 4, Compiled = 8, Singleline = 16, IgnorePatternWhitespace = 32, RightToLeft = 64, ECMAScript = 256, CultureInvariant = 512, NonBacktracking = 1024, }
| Option | Effect |
|---|---|
IgnoreCase |
Case-insensitive matching. |
Multiline |
^ and $ match at the start and end of each line. |
Singleline |
. also matches \n. |
IgnorePatternWhitespace |
Unescaped whitespace in the pattern is ignored; # begins a comment. |
ExplicitCapture |
Only named groups capture; (x) acts like (?:x). |
RightToLeft |
Search from the end of the input toward the start. |
CultureInvariant |
Case folding ignores the current culture. |
ECMAScript |
JavaScript-like behavior (ASCII \w, \d); only combines with IgnoreCase, Multiline and Compiled. |
Compiled |
Generate IL for the pattern; slower to construct, faster to run. |
NonBacktracking |
Linear-time engine; no backreferences, atomic groups or lookarounds. |
Errors / undefined behavior: an invalid combination (for example ECMAScript with Singleline) throws ArgumentOutOfRangeException. NonBacktracking throws NotSupportedException for unsupported constructs.
Usage notes
Multilinedoes not mean "many lines". It only changes^and$. To let.cross lines you needSingleline; a typical "whole block" pattern uses both.$and Windows line endings. InMultilinemode$matches before\nonly, so lines ending in\r\nneed\r?$.IgnoreCaseand culture. WithoutCultureInvariantthe current culture's casing rules apply (the Turkish I is the classic trap). PassCultureInvariantfor data that is not human language. Our runner is already invariant.Compiledis a trade. It costs milliseconds at construction and memory for the generated code. It pays off for patterns that run thousands of times in a long-lived process. For fixed patterns,[GeneratedRegex]is usually better.NonBacktrackingis the safe option for untrusted patterns or inputs; it is immune to catastrophic backtracking. Captures work, but results of some patterns can differ slightly from the backtracking engine.ExplicitCapturesimplifies grouping. With it you can use plain parentheses for structure and still get tidyGroups.RightToLeftchanges the order of evaluation, not just the direction: quantifiers and lookarounds are applied from the right, which can change which match wins.- Combine with
|:RegexOptions.IgnoreCase | RegexOptions.Multiline. Using+also works for distinct flags but|is the convention.