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.
In this topic:
- Regular expressions and strings
- Highlighting
- Fixing errors
- Validating and testing
- IntelliSense
- Extracting precompiled regular expression
Regular expressions and strings
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, etc.
If you want ReSharper to process these strings as regular expressions, you have two options.
One way is to mark any string as a regular expression using a context action.
To process regular expressions outside of Regex class methods
- Set the caret on a string containing regular expressions
- Press Alt+Enter or click on the action indicator to the left of the caret to open the action list.
- Choose the Make regular expression here context action.
- If you decide later to disable processing the string as a regular expression, you can use the Remove regular expression context action.
Another way is to annotate parameters of your own methods as regular expressions using the
[RegexPatternAttribute]
from
JetBrains.Annotations
.
To process regular expressions in method arguments
-
Reference the
JetBrains.Annotations
namespace in your project. For more information, see Annotations in Source Code -
Annotate a parameter that should receive a regular expression with the
[RegexPatternAttribute]
.
ReSharper will process the corresponding arguments in method calls as regular expressions:

Highlighting
ReSharper highlights syntax constructs as well as errors and redundancies in 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 in ReSharper Options | Environment | Editor | Editor Appearance.
By default, ReSharper highlights correct and incorrect escape sequences in all non-verbatim strings:

If necessary, you can turn this highlighting of by clearing the Highlight special characters in string literals check box on the Code Inspection | Settings options page.
Fixing errors
To fix errors in regular expressions, set 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.

ReSharper helps you fix the error automatically:

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

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).
To validate a regular expression in your code
- Set the caret at a regular expression.
- Press Alt+Enter or click on the action indicator to the left of the caret to open the action list.
- Choose the Validate regular expression context action.
-
In the
Validate Regular Expression
dialog box 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 check box. Note that in this case, the samples should be single-line strings. - If necessary, you can change engine the regular expression options in the Options drop down list.
- 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.

In regular expressions, you can use four types of IntelliSense:
-
Automatic completion
- triggered after the
\
,(
and[
characters - Basic completion (Ctrl+Space) – shows elements, available for the current scope
- Smart completion (Ctrl+Shift+Space) – shows most probable elements for the current scope
- Double completion ( Ctrl+Space twice) – shows all possible elements
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:

Extracting 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, set 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/technologies:
C# | VB.NET | C++ | HTML | ASPX | Razor | JavaScript | TypeScript | CSS | XML | XAML | RESX | Build Scripts | Protobuf | JSON |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
![]() |
![]() |
![]() |