Bewerken

Delen via


Invalid regex pattern (RE0001)

Property Value
Rule ID RE0001
Title Invalid regex pattern
Category Style
Subcategory N/A
Applicable languages C#, Visual Basic
Options See Options section.

Overview

This rule flags places where a literal string containing a regular expression does not conform to required syntax. The rule applies to an identified regular expression, which is determined by the inclusion of an inline comment or comment preceding the regex string, such as //lang=regex or //lang=regex,strict. The latter comment uses strict mode for regex interpretation.

This rule is IDE-only and not applicable for command-line scenarios.

Options

Options specify the behavior that you want the rule to enforce.

For code recognized as regex, you set the options in Visual Studio by selecting Tools > Options > Text Editor > C# | Visual Basic > Advanced. The following options are available under the Regular Expressions section:

Property Description
Colorize regular expressions Specifies whether to colorize regular expressions.
Report invalid regular expressions Specifies whether to report invalid regular expressions.
Highlight related components under cursor Specifies whether Quick Actions
uses highlighting.
Show completion list Specifies whether to show IntelliSense code completion.

Example

The following code snippet shows an example with an invalid regular expression.

// Code with violations
// lang=regex
string pattern = @"\b[M]\w+\";

// Fixed code
// lang=regex
string pattern = @"\b[M]\w+";

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable RE0001
// The code that's violating the rule is on this line.
#pragma warning restore RE0001

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.RE0001.severity = none

For more information, see How to suppress code analysis warnings.