Regular Expressions Assistance
JetBrains Rider 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, JetBrains Rider helps with automatic completion and validation.
Regular expressions in string literals
By default, JetBrains Rider 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 JetBrains Rider 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.
JetBrains Rider 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 as regular expressions using the
[RegexPatternAttribute]from JetBrains.Annotations. This is the recommended way for regular expressions in parameters.
JetBrains Rider will process the corresponding arguments in method calls as regular expressions:
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 gel lost, and anyone opening your code with JetBrains Rider will get the same features in the marked strings. By the way, the format of comments is compatible with IntelliJ Platform-based IDEs.
JetBrains Rider 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.
By default, JetBrains Rider highlights correct and incorrect escape sequences in all non-verbatim strings:
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.
JetBrains Rider helps you fix the error automatically:
JetBrains Rider 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
Basic completion Ctrl+Space – shows elements, available for the current scope
Smart completion Ctrl+Alt+Space – shows most probable elements for the current scope
You can also benefit from JetBrains Rider's Intellisense when using the Match.Groups Property. JetBrains Rider detects group names in the expression and suggests them in the completion list:
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, 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:
After applying the context action, the pattern is extracted into a static field: