Andrii Raikov

How Regex Works: The Secret Sauce Behind Pattern Matching

What if you could build a regex engine from scratch? Learn how to translate any pattern into a simple state machine for lightning-fast matching.

How Regex Works: The Secret Sauce Behind Pattern Matching
#1about 4 minutes

Solving a LeetCode regex matching challenge

A LeetCode problem requiring a custom regex matcher for the dot and asterisk characters serves as the motivation for exploring how regex engines work.

#2about 2 minutes

Understanding the scalability limits of a naive approach

The initial linked-list implementation is not extensible enough to handle complex patterns like a real-world email validation regex.

#3about 3 minutes

How regex works using finite automata

Regular expressions can be modeled as finite automata, or state machines, which describe a set of character strings through states and transitions.

#4about 2 minutes

Comparing deterministic and non-deterministic finite automata

Non-deterministic Finite Automata (NFA) are more flexible for representing regex because a single input can lead to multiple possible next states.

#5about 5 minutes

Building an NFA from regular expression components

A complex regular expression is converted into an NFA by combining smaller NFA components for concatenation, alternation, and repetition operators.

#6about 2 minutes

The performance pitfalls of the backtracking algorithm

The backtracking approach, where the engine guesses a path and reverts on failure, can lead to exponential time complexity for complex patterns.

#7about 1 minute

Simulating an NFA by tracking all possible states

A more efficient simulation method avoids backtracking by simultaneously tracking the entire set of possible states the machine could be in at any point.

#8about 5 minutes

Implementing a simple NFA regex matcher in Go

A practical implementation involves compiling the regex pattern into an NFA data structure and then iterating through the input string while tracking the current set of active states.

#9about 2 minutes

Beyond one algorithm for real-world regex engines

Production-grade regex engines often use a hybrid approach, selecting from different algorithms like NFA simulation or backtracking based on the specific pattern.

Related jobs
Jobs that call for the skills explored in this talk.

Featured Partners

Related Articles

View all articles
BR
Benjamin Ruschin
What Developers Really Need to Create Great Code Demos
Every developer on earth has, at some point, had another developer to thank for a breakthrough, a success, an aha moment they wouldn’t have had without coming across that blog post, that open-source contribution, that reply on socials or that humble ...
What Developers Really Need to Create Great Code Demos
CH
Chris Heilmann
With AIs wide open - WeAreDevelopers at All Things Open 2025
Last week our VP of Developer Relations, Chris Heilmann, flew to Raleigh, North Carolina to present at All Things Open . An excellent event he had spoken at a few times in the past and this being the “Lucky 13” edition, he didn’t hesitate to come and...
With AIs wide open - WeAreDevelopers at All Things Open 2025
DC
Daniel Cranney
Dev Digest 195: End of Likes, JavaScript’s a Zoo, and Messing with Bots!
Inside last week’s Dev Digest 195 . 👎 No more external likes 🤗 Needy programs 📉 The worst selling Microsoft product 🟨 JavaScript engines zoo 🍞 No more toasts! 🤖 Messing with bots 👔 Beware of fake job interviews 🗞️ Join over 150,000 developers alread...
Dev Digest 195: End of Likes, JavaScript’s a Zoo, and Messing with Bots!
DC
Daniel Cranney
Dev Digest 194: AI vs. Version Control, Password Louvre & Cursed Webdev
Inside last week’s Dev Digest 194 . 🧠 Learn how to become an AI-native software engineer 🤷‍♂️ How can you stand out when anyone can build anything? 👂 Whisper Leak allows listening to encrypted chats 🐝 What’s new the OWASP2025 Top Ten List 🙅‍♀️ Curse...
Dev Digest 194: AI vs. Version Control, Password Louvre & Cursed Webdev

From learning to earning

Jobs that call for the skills explored in this talk.

Software Engineer

Software Engineer

Understanding Recruitment
Charing Cross, United Kingdom

Go
Java
Python
Software Engineer

Software Engineer

Understanding Recruitment
Charing Cross, United Kingdom

React
Python
Machine Learning
Google Cloud Platform
Amazon Web Services (AWS)
Software Engineer

Software Engineer

Understanding Recruitment
Municipality of Madrid, Spain

Remote
150-200K
API
C++