Write Regular Expressions Easily with Rider
Regular expressions enable people to locate data inside of strings based on pattern matching. Regardless of the programming language, regular expressions (RegEx), are quite useful and powerful, though often disliked. That’s because they have a reputation for being difficult to work with.
If you’re using Rider to code regular expressions, it’s not difficult at all! In this post we’ll take a look at how you can effortlessly write regular expressions with Rider.
Readability is key for producing quality code. Rider displays metadata about the arguments for many types of methods, and that includes methods on objects that compute regular expressions.
As you can see in the following example, Rider lets you know which argument is the
matchTimeout. Otherwise, it’s difficult to remember which arguments of a method are which, making Rider’s parameter hints quite helpful.
Rider does a wonderful job of highlighting colors, so your eyes can easily scan the code to see how any particular pattern is formed. The ReSharper Light theme default colors have the most clarity and variety, as the example below demonstrates:
The color meanings will vary depending on the chosen theme, but are customizable in Rider’s Options/Preferences under Editor | Color Scheme | RegExp. You can set a theme or individual colors specifically for regular expressions that is different from Rider’s general theme.
Code Completion for Regular Expressions
Because it’s impossible to remember each and every character literal, operator, and construct that you might want to use, Rider provides excellent code completion for regular expressions.
Start typing your expression, and choose from the list of constructs that show in the code completion list. It’s that easy! Think about the things you want to match, and you can let Rider do the rest.
Better Code Analysis with Annotations
By default, Rider recognizes regular expressions in the pattern parameter and methods of the
Regex class. This means that many regular expressions you might use won’t have color coding or code completion. That’s because it’s difficult for tools (and developers!) to understand exactly what any particular string is supposed to be. Is it supposed to be a numeric string like some postal codes? Or is it something different – an email, perhaps?
Often, we must parse the string and do a bit of investigation ourselves to find out what is really in that string. This can be quite a manual and tedious process. But if you use the annotations from
JetBrains.Annotations, you can specify that the string is a regular expression pattern. Then you can take advantage of all the great regular expression features that Rider has to offer! For example, it’s common to pass around strings as arguments to methods that process regular expressions, like so.
As you can see, once the
[RegexPattern] annotation is used, Rider can apply syntax highlighting and other features that make working with regular expressions fun and easy.
Alternatively, you can use a
/*language=regexp|jsregexp*/ comment to note that the next line of code is a regular expression.
Use the NuGet tool window or the Terminal in Rider to install the
Dealing with Errors
Trying to translate an idea of a pattern in your head into a regular expression in an editor is a difficult job. Therefore, errors will happen even to the best of us, and even when we have an excellent tool like Rider helping us along.
But with Rider, dealing with errors is significantly easier. All you have to do is press Alt+Enter when you see the error squiggles and you’re set – Rider will suggest a fix for you.
Download Rider and give it a try! We would love to hear your feedback!
Subscribe to Blog updates
Boost Code Quality with Qodana and GitHub Actions
It’s been roughly half a year since we introduced Qodana to .NET in our blog post about how to elevate your C# code quality with Qodana. Since then, we’ve been quite busy! Qodana went out of preview and into GA. Furthermore, we greatly improved the integration with our IDEs, providing an effortless …
Critical Thinking in an AI-Powered World
Critical thinking techniques for use with JetBrains AI Assistant.
How To Use AI Assistant to Generate Test Data For .NET Applications
How To Use AI Assistant to Generate Test Data For .NET Applications for better unit tests.