ReSharper 2024.3 Help

Regular expressions assistance

ReSharper provides a rich set of tools to work with .NET regular expressions. You can quickly analyze existing expressions, find and fix errors. When typing new expressions, ReSharper helps with automatic completion and validation.

Regular expressions in string literals

By default, ReSharper only processes regular expressions in the pattern parameter, in methods of the Regex class. However, strings containing regular expression can be defined in different places: string constants, fields, other methods' arguments, and so on. If you want ReSharper to process a string as a regular expression, you have three different options:

  • Use a context action: press Alt+Enter while your caret is in the string and choose Mark as .NET regular expression.

    ReSharper will mark the symbol range corresponding to the string as regular expression, save this range in its internal database and will be keeping track of it as the containing file changes. This way is very quick and straightforward, but there are two downsides: the range can be lost after external file change, such as VCS merge, and the injection marked this way will only be tracked locally.

    If you decide later to disable processing the string as a regular expression, you can use the Remove .NET regular expression mark context action.

  • Another way is to annotate parameters of your own methods, public fields, or properties as regular expressions using the [RegexPatternAttribute] from JetBrains.Annotations.

    If your project targets .NET 7 or later, you can also use the [StringSyntaxAttribute] with the corresponding constructor: [StringSyntax(StringSyntaxAttribute.Regex)].

    ReSharper will process the corresponding arguments in method calls and assignments as regular expressions:

    Highlighting regular expressions in arguments
  • The third way is a comment /*language=regexp|jsregexp*/ before the string literal. These comments require some typing and maybe contaminate your code, but on the other hand, they make your intentions clear to everyone who reads your code, they won’t get lost, and anyone opening your code with ReSharper will get the same features in the marked strings. By the way, the format of comments is compatible with IntelliJ Platform-based IDEs.

    Regex injection in C# string with comment

Syntax highlighting

ReSharper highlights syntax constructs as well as errors and redundancies in regular expressions:

Highlighting of regular expressions

Highlighting colors have the following meanings:

  • Light Blue – character classes, anchors and quantifiers

  • Light Green – grouping constructs

  • Orange – set constructs

  • Pink and light pink – escape sequence

  • Green – comments

  • Red with curly underline – errors

  • Blue curly underline – warnings

Matching brackets in groups, group names and sets are highlighted when you set a caret to one of the delimiters. You can toggle and adjust this highlighting using the Highlight matching delimiters setting on the Environment | Editor | Appearance) page of ReSharper options.

By default, ReSharper highlights correct and incorrect escape sequences in all non-verbatim strings:

Highlighting of escape sequence in a string

If necessary, you can turn this highlighting off by clearing the Highlight special characters in string literals checkbox on the Code Inspection | Settings page of ReSharper options.

Fix errors

To fix errors in regular expressions, place the caret at the red highlight, press Alt+Enter, and then select the corresponding quick-fix.

The most common example of a regular expression error is a misuse of the escape characters.

Regular expression error

ReSharper helps you fix the error automatically:

Fixing regex error with ReSharper

Validate and test

ReSharper allows you to validate and test your regular expression patterns at design time or while debugging. In the Validate Regular Expression dialog, you can enter various sample strings and see how your regular expression matches these strings. The dialog is available in the main menu: ReSharper | Tools | Validate Regular Expression…. Using this dialog, you can fix your expression and make sure that you get the desired matching.

Validating regular expressions

ReSharper applies the standard .NET regular expression engine for processing ao the expression works exactly the same way as it would in runtime. All matches in sample strings are highlighted.

Also, the matches are displayed in a tree view by matches, groups in matches and all captures for groups (if there are more than two of them). You can select nodes in the tree to highlight corresponding parts of the sample strings and groups in regular expression (if a group or capture is selected).

Validate a regular expression in your code

  1. Place the caret at a regular expression.

  2. Press Alt+Enter or click the action indicator to the left of the caret to open the action list.

  3. Choose the Test regular expression context action.

  4. In the Validate Regular Expression dialog that opens, provide some sample strings in the Test Input area.

    To test several sample strings simultaneously, separate the strings with a new line and check the Check lines separately checkbox. Note that in this case, the samples should be single-line strings.

  5. Use the Options list to set the regular expression options that will be used when validating your expression against the test input in the dialog.

  6. The Matches table will list all matches of your regular expression in the test input. You can click the matches to locate them in the test input.

  7. If the regular expression works as expected, click Insert to insert it back in the code.

IntelliSense

ReSharper provides IntelliSense support for almost all .NET regular expression constructs. In the completion list, each construct is shown with a brief description.

Code completion in regular expressions

In regular expressions, you can use the following types of IntelliSense:

You can also benefit from ReSharper's Intellisense when using the Match.Groups Property. ReSharper detects group names in the expression and suggests them in the completion list:

Group names completion

Extract precompiled regular expression

If you need to reuse a regular expression, which is used in a static method of the Regex class, you can extract it to a precompiled regular expression.

To extract the regular expression, place the caret anywhere in the method call, press Alt+Enter and choose the To precompiled Regex context action.

For example, you can extract the regular expression from the pattern parameter of the IsMatch method:

public void Bar() { var result = Regex.IsMatch("Input", "Pattern"); }

After applying the context action, the pattern is extracted into a static field:

private static readonly Regex Regex1 = new Regex("Pattern"); public void Bar() { var result = Regex1.IsMatch("Input"); }

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is not available in Visual Basic

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is available in JavaScript

Feature is available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is available in JSON

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the ReSharper by language section.

Last modified: 25 September 2024