Validate Regex in IntelliJ IDEA

Jun 15th, 2022

2 min read

kotlinregexintellij

Here follows a mildly embarrassing TIL post.

Regular expressions can be tough, both to write and to read. There's an entire folder in my bookmarks dedicated to regex articles, testers, and self-guided courses. In spite of this, I still end up doing the whole plug-and-test routine after writing a new regex, no matter how simple it is.

One random Tip of the Day later, and I discovered that regular expressions can be validated directly inside my IDE, IntelliJ IDEA, by using a single intention action. And this tool has been available since at least 2011 🙂💀.

It's a 🍰 too.

Place the caret at the regex that you've been working on for the last hour ten minutes, press Alt+Enter (or click the intention bulb if not disabled), then select Check RegExp from the menu of intention actions, and provide single or multiline strings to search for a match.

The checker produces a green tick mark if at least one match is found and highlights all matches:

Snapshot of regex checker finding matches in multiple strings

Or shows a warning sign if a match is not found as typing continues:

Snapshot of regex checker finding no matches in 3 provided strings

Or a red error symbol if a full match is not found, for example when using start- and end-of-string anchors:

Snapshot of regex checker evaluating a full string as having no complete match

The tool works regardless of whether the regex object is created using the Regex constructor or the function toRegex(), as long as the caret is placed appropriately.

Now excuse me while I go delete a couple of bookmarks. Not this one though (the explanation step-through alone makes it a solid keeper).


Thanks for reading!🛠️🤦

|© 2023 bog-walk