Learn Regex the Easy Way, Part 4: Quantifiers
Quick Recap
In Part 3, we covered character classes: square brackets that match one character from a set, POSIX classes like [[:digit:]], and negation with [^...]. Now let’s talk about how many characters to match.
The Four Core Quantifiers
A quantifier tells the regex engine “how many of the previous thing do I want?” There are four you’ll use constantly:
Quantifier Meaning Example * Zero or more ab*c matches ac, abc, abbc + One or more ab+c matches abc, abbc, but NOT ac ? Zero or one (optional) colou?r matches color and colour {n} Exactly n a{3} matches aaaA Critical Detail: What Gets Quantified
The quantifier applies to the thing directly before it. This is probably the most common beginner mistake.
abc+ Matches: ab followed by one or more c's
(abcc, abccc, etc.)
Does NOT mean "one or more abc"
If you want “one or more abc,”...