DEV Community

Cover image for How to Learn Regex (and Stop Googling the Same Patterns)
Spartan Rich
Spartan Rich

Posted on

How to Learn Regex (and Stop Googling the Same Patterns)

I have looked up the regex for "match an email address" at least a dozen times in my life. Not because it's hard, but because every time I needed it, I copied something from Stack Overflow, it worked, and I learned nothing. Six months later, same search, same tab.

If you're searching "how to learn regex," you've probably been through the same loop. The good news is that regular expressions are a small language. The core fits on an index card. What's missing for most of us is the second half of learning: seeing the pieces again until they stop feeling foreign.

Learn the five building blocks first

Almost every regex you'll write in a normal week is built out of five things.

Literal characters match themselves. cat matches "cat".

Character classes match one character from a set. [aeiou] is any vowel, [0-9] is a digit, and [^0-9] (with the caret inside) is anything that isn't a digit. The shorthands \d, \w and \s mean digit, word character and whitespace. Their capital versions, \D, \W and \S, flip them.

Quantifiers say how many. * is zero or more, + is one or more, ? is zero or one, and {2,4} is between two and four.

Anchors match positions, not characters. ^ is the start of the line, $ is the end, and \b is a word boundary. People skip anchors and then wonder why \d{3} matches inside "12345".

Groups and alternation. Parentheses group things, | means "or". (cat|dog)s? matches cat, cats, dog and dogs.

That's it. Lookaheads, named groups, backreferences and flags are all useful, but they're extras on top of these five. Learn the five until you can read them without thinking, then add one extra at a time.

Read regex out loud

This sounds silly and it works. Take ^\d{3}-\d{4}$ and say it: "start of line, three digits, a dash, four digits, end of line." If you can't say a piece, that's the piece you don't know yet.

Do this with regexes you find in your own codebase. Every project has a few in validation code, log parsing, or a route definition. They're real, they're in context, and someone on your team already decided they were worth writing.

Use a tester, and read the explanation panel

regex101 is the tool I'd point anyone to. Paste a pattern and some test text, and it highlights matches and explains every token in plain English on the side. Pick the right flavor in the left menu (PCRE, JavaScript, Python, Go and others), because they differ in small ways. JavaScript, for example, only got lookbehind assertions in ES2018, and Go's RE2 engine doesn't support them at all.

The trap with testers is the same as with Stack Overflow. You fiddle until it turns green and move on. Before you close the tab, read the explanation panel once from top to bottom. That one minute is where the learning happens.

Know the two gotchas that bite everyone

Greedy matching. .* grabs as much as it can. Run <.*> on <b>hi</b> and you get the whole string, not <b>. Add a ? to make it lazy: <.*?> stops at the first >.

Escaping. . means "any character," so example.com also matches "exampleXcom". If you mean a literal dot, write \.. The same goes for ?, +, *, (, ), [, {, |, ^, $ and the backslash itself. In many languages you also need to escape the backslash in the string literal, which is why raw strings like Python's r"\d+" exist.

Once you've been burned by each of these once, you'll spot them. Before that, they eat afternoons.

Turn your lookups into flashcards

Here's the habit that actually fixed my email-regex problem. Every time I have to look something up, I write it down as a question I could be quizzed on, with the answer on the back.

What does \b match?
A word boundary: the position between a word character and a non-word character. It matches no actual characters.

How do you make + lazy?
Add ? after it: +?

What does (?=...) do?
A positive lookahead. It checks that the pattern ahead matches, without consuming it.

Then I review those cards on a growing schedule: the next day, a few days later, a week, two weeks. That's spaced repetition. You see the cards you keep missing more often and the ones you know almost never. I wrote up how the scheduling works in How to Implement Spaced Repetition (SM-2) in JavaScript if you'd rather build your own than use Anki.

Keep the deck small and personal. Thirty cards built from things you actually looked up are worth more than a 300-card deck someone else made.

Save the good explanations so they come back

A lot of my regex learning came from conference talks and YouTube videos where someone walks through a nasty pattern step by step. Those are great, and then they vanish into Watch Later.

That's part of why I built Recl. You share a video, reel, podcast or article to it from your phone or the Chrome extension, and it writes a summary of the key points, with timestamps for videos. It sorts saves into topics, so the regex and text-processing stuff ends up in one place, and it brings each save back to you on a spaced schedule (1, 3, 7, 14, 30 and 60 days later) so the explanation shows up again before you've forgotten it.

Recl feed showing saved items with summaries

Practice on things you already do

You don't need regex puzzles. Use it in the tools you're already in. Search your codebase with rg or your editor's regex search instead of plain text. Rename a batch of variables with a find-and-replace that uses a capture group, like replacing get_(\w+) with fetch_$1. Pull every IP address out of a log file with grep -E. Each of those takes two minutes and teaches more than an hour of exercises, because you cared about the result.

A reasonable pace is one new feature a week. Week one, anchors and boundaries. Week two, capture groups and backreferences. Week three, lazy quantifiers. Week four, lookaheads. By the end of the month you'll read most patterns in a code review without opening a tester, and the email regex will be yours, not something you borrowed again.


I build Recl, an AI learning app that saves your videos, reels, podcasts and articles, summarizes them, sorts them into topics, and uses spaced repetition so you remember what you learned.

Top comments (0)