Member-only story
Demystifying Look-Ahead and Look-Behind in Regex
Simplifying look-ahead and look-behind in Regular Expressions with easy-to-understand examples and animated GIFs

One of the concepts in Regular Expressions (Regex) that I’ve always found difficult to wrap my head around is look-arounds — which comprise look-aheads and look-behinds.
While there are plenty of articles and tutorials online explaining this concept, few do it in a way that is easy to understand, at least not to my satisfaction. Many use jargons such as “consuming groups,” “zero-width assertions,” etc., which doesn’t help those who are learning this advanced topic.
Furthermore, there is a lack of clarity over how to interpret the names of the look-arounds. For instance, for look-behind, what is “behind" relative to? What are we “looking" for? The same goes for look-ahead. As if they are not confusing enough, there are two sub-types — positive and negative — for each type of look-around.

In this article, I attempt to demystify the concepts of look-ahead and look-behind once and for all. I will avoid technical jargon and instead explain in simple terms, supported by animated GIFs.
Postscript: For optimal viewing of animated GIFs, I’d recommend using a desktop web browser.
My explanation will be programming language-agnostic, although my code snippets will be in Python. I hope this article will be useful for you. Let’s begin!
The code snippets and animated GIFs shown in this article can be found at this GitHub repo.
1. A Primer on Regex Look-Arounds
Before we dive deeper, let’s first get some high-level intuition of what look-arounds are trying to achieve and how they work. Let’s do this with a simple analogy.
Suppose you’re a tourist in another country and you wish to visit a local museum. You’re on foot and you’re lost. You ask a passerby for directions to the museum. They tell you, “Go straight ahead, and once you see the French café on your…