How can I improve at writing RegExp?

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Creating a regular expression is the weak point of many developers, but how can you improve?

Article published on 18/03/2021, last updated on 10/08/2026

A regular expression (also called a RegExp) is a sequence of characters following a very specific syntax and representing a textual search pattern.

In other words, thanks to a RegExp, you can search in any text to see if a particular pattern is present.

For example a string starting with http:// or https:// to detect a link.

Once a string of characters matching the pattern is found, it is possible to retrieve it, modify it, or replace it.

The problem with regular expressions is that the syntax can be scary and quite complicated to read when we're not very well trained.

And even with training, it takes a few seconds to understand the meaning of /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$/

And yet, a well-written RegExp can make a difference in terms of performance and maintainability. So if you'd like to improve, here are some tips:

Get motivated

It is by forging that one becomes a blacksmith, well it's also by writing RegExps that we improve; the first step is therefore to force yourself to take the time to try this exercise in real conditions.

The two main pitfalls are:

  • Going to search the web for ready-made RegExps
  • Turning to a procedural algorithmic solution (based on if, else, for, etc...) that you already know

Even if it's not always easy, and you will surely waste time the first few times, this is part of learning the developer craft, and performance will be there to reward you!

Nothing then stops you from going to see what others have done, I'm not telling you to reinvent the wheel every time, but at least to try and not fall into the easy way out in order to progress.

Break it down

Like any problem that seems insurmountable, you must break it down into smaller sub-problems that seem manageable at your level.

Start by trying to roughly delimit your pattern, select one character, then two, then another special character, and finally add a multiplicity rule....

All these steps can be accomplished by simply keeping in front of you a list of the references of your RegExp engine (depending on your language), and having a test set for each new step.

Write and test

For this phase, I simply have a tool that saves my life every time I have to write a new regular expression.

This tool is a free site called regex101 which, in a single workspace, allows you to:

  • Choose your RegExp engine (language)
  • Write your regular expression
  • Test it live, with details of the matches between the test strings and the patterns
  • Have access to the reference documentation

And even get a natural language transcription of your RegExp pattern (in English). If you should only use one reference site, it's this one!

Platform link: https://regex101.com/

Play

There is also a crossword puzzle game based on regular expressions logically called "Regex Crossword" to practice while having fun.

I didn't know this game, but since it was recommended to me several times by readers, I'm sharing it here!

And otherwise

If you develop in Javascript, there is an open-source project called SuperExpressive that allows you to create regular expressions from a higher-level API: https://github.com/francisrstokes/super-expressive


Ryan Snaadt sur Unsplash

Finished reading this article?
Our newsletter

No spam. Only free content, news, and ever more resources to level up your skills!

Join +1500 developers

Comments (0)

to leave a comment

No comments yet