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.

Regular Expressions highlighting in ReSharper 9

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:

Matching Braces for Regular Expressions in ReSharper 9

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:

Error highlighting for Regular Expressions in ReSharper 9

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:

Missing Brackets highlighting in Regular Expression in ReSharper 9

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:

code completion for regular expressions in ReSharper 9

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:

Code completion for MatchResult objects in ReSharper 9

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:

Errors with backslash in regular expression

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:

Quick-fix to turn the string into a verbatim string in ReSharper 9

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:

Regular Expressions validation utility in ReSharper 9

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:

Regular Expression match options in ReSharper 9

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:

Quick Documentation in Validation utility in ReSharper 9

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:

Context Action on all regular expression strings in ReSharper 9

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 the Regex.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:

To precompiled regex context action in ReSharper 9

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!

This entry was posted in ReSharper Tips&Tricks and tagged , . Bookmark the permalink.

9 Responses to Regular Expression Support in ReSharper 9

  1. Eyal Shilony says:

    This is pretty amazing, good job! :)

  2. Pingback: Wow, really great RegEx support coming with ReSharper 9 | Jesse's Blog

  3. Pingback: Regular Expression enhancements in ReSharper 9 | In Absentia

  4. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1724

  5. Uwe says:

    Isn’t “precompiled” the wrong word for what you are doing?

    In my opinion only when using RegexOptions.Compiled, one can speak of a precompiled regex.

    See also this Stack Overflow answer.

  6. Pingback: Доступны для скачивания ReSharper 9, dotTrace 6, dotCover 3, dotMemory 4.2 и dotPeek 1.3 | Zit@i0

  7. Andy says:

    Can R# now also highlight escaped characters in normal strings? e.g.
    If not, take my feature request!

Leave a Reply

Your email address will not be published. Required fields are marked *