This article is part of a Series On Regular Expressions.
In this article, we will discuss regular expression quantifiers with the ultimate goal of developing a very deep understanding of how they work. In fact, by the end of this article you'll understand quantifiers so well, that you'll be able to extend your knowledge to invent a completely new quantifier has never been seen before in any regular expression engine.
By the end of this article, you will understand everything in the following diagram:
Quantifiers are used to quantify how many times a part of your regular expression should be repeated. Every time you want to repeat something in a regex (an individual character, a character class or a sub-expression) you can write a quantifier after it to specify how many times it should be repeated.
The following list shows some examples of the most common quantifiers:
This article will only discuss these quantifiers within the context of Perl-like regular expressions (not POSIX regular expressions). This is worth mentioning since the older POSIX style regular expression quantifiers lack certain features, and have significantly different behaviour in some cases.
Most typical guides to regular expressions would probably start by immediately diving into a detailed description of what each of these symbols does. They would probably start by explaining the meaning of the '*', '+', and '?' symbols. However, this won't be your typical guide to regular expressions.
Instead, we'll start by focusing on understanding this quantifier:
{N,M}
The reason for this atypical approach is simple: Once you understand what this quantifier does, you're already 50% done learning everything there is to know about quantifiers. Let's do a few examples.