.NET Tools
Essential productivity kit for .NET and game developers
Regular Expression Support in ReSharper 9
If you’ve ever used regular expressions in practice, chances are you’ve been dismayed at their cryptic nature and frustrated by having to download a separate application just to make heads or tails of them.
Well, in ReSharper 9, we are adding support for regular expressions, helping you both read regular expressions and offering helpful diagnoses of potential problems.
Expression Highlighting
To make regular expressions more readable, ReSharper now uses different colors to highlight different parts of a regular expression.
We have introduced the following colors for different parts of regular expression syntax:
- Light Blue — character classes, anchors and quantifiers.
- Light Green — grouping constructs
- Orange — set constructs
- Pink and Light Pink — escape characters (these alternate for better readability)
- Green — comments
- Red — used to indicate errors
In addition to the color highlighting, we also highlight matching braces in groups, group names and sets:
ReSharper is capable of highlighting invalid characters and escape character sequences, and also uses different highlightings for adjacent elements so as to make the expression more readable.
Error Highlighting
It wouldn’t be ReSharper if you couldn’t get analysis of your regular expressions, would it? That’s why, in ReSharper 9, regular expressions are checked and validated, and any errors are indicated in piecewise fashion: instead of just flagging the whole string as erroneous, ReSharper carefully highlights only the parts of the regular expression string that actually constitute an error:
Some more examples of the kind of errors ReSharper can pick up on, including missing brackets or group names as well as invalid qualifiers that appear after an anchor character or a group name:
Code Completion
ReSharper already provides plenty of context-sensitive code completion within string literals. For example, completion in ToString()
function calls helps you enter format specifiers. So, with regular expressions, we did exactly the same thing by providing code completion for known regular expression escape sequences:
This form of completion is useful as a form of quick documentation, as in addition to the sequences themselves, ReSharper also shows textual descriptions.
As with other code elements, ReSharper provides the following completion mechanisms:
- Basic completion shows elements that are available for the current scope
- Smart completion shows the most relevant elements for the current scope
- Double completion shows all possible elements
- Automatic completion gets triggered after the user enters one of the
\
,(
or[
characters.
In addition to code completion within regular expressions, an additional benefit of ReSharper knowing about regular expressions is code completion for MatchResult
objects: you now get explicit completion for group names within such a result:
Naturally, you also get code completion within the group name string itself (i.e. Groups["<here>"]
).
Escaping Quick-Fix Helper
Quite often, when you write a regular expression, you want to write something like the following:
Regrettably, this will not compile since the backslash (\
) characters themselves need to be escaped, making the sequences \w
and \s
invalid (as opposed to \\d
, which is correctly escaped). To help with this, ReSharper provides a quick-fix that turns the string into a verbatim string:
Notice that the \\d
from the sample code has been turned into a \d.
As always, ReSharper preserves the fidelity of the strings when changing the way they are presented.
Validation Utility
Having a separate little applet that validates regular expression is still very useful, which is why ReSharper is now providing one of its own:
As soon as the input text is modified, the standard .NET regular expression engine processes the expression and attempts to match it against the test input. All the located matches are highlighted in light green right in the text editor.
If there is a match, the exact match groups are indicated in the tree on the right of the window. The tree shows matches, groups in matches and all captures for groups (if there are more than two). Furthermore, if we select any element in the tree, the corresponding part of input text and the group (if any) of a regular expression are also highlighted.
Finally, the Check lines separately lets us test several input lines independently. This option becomes available only when there is more than one line in the input box, and test each of the single lines individually.
Finally, the typical regular expression match options are available from a drop-down check list:
The validation utility provides quick documentation for each expression option: simply hover the mouse over a question mark next to the option and you’ll get a textual explanation of what this option does:
To make it easy and convenient to use the Validation Utility, ReSharper now offers a corresponding context action on all regular expression strings:
Conversion to Precompiled Expression
Quite often, we use a static Regex
member (e.g., Regex.IsMatch()
), for example:
At some later point, we might want to turn this static call into an instance call, i.e., to precompile the expression and save the resulting finite state machine in a static field. Of course, this is only possible if the pattern does not depend on any input data.
Why would we want to do this? The reasons are obvious:
- We want to reuse a regular expressions in other locations in our code; or
- We want to use a great many regular expressions through static calls. According to
Regex
documentation, this class only caches a fixed number of expressions, the exact amount delineated by theRegex.CachedSize
variable (default value is 15).
Thus, ReSharper provides a helper action called To precompiled regex that translates a static call into a precompiled regular expression. After invoking the action, we get the following:
Check it out!
So here it is: Regex support in ReSharper 9. To try it out right now, give ReSharper 9 EAP a go and let us know what you think!