Regex Quantifiers

Quantifiers lets you match sequences.

Name Quantifier Character Effect
Zero or More * Matches zero or more occurrences of the pattern to its left
One or More + Matches one or more occurrences of the pattern to its left
Zero or One ? Matches pattern that either occurs once or doesn't occur at all
Ranges p{m,n} Matches matches `m` or more occurrences of `p`, but not more than `n` if provided

Greediness vs. Laziness

The quantifiers above are greedy: they always match the longest possible string they can.

Greediness is uselful most cases, but when you need to match the fewest number of characters possible, we need to use a lazy match.

In Ruby and Javascript, lazy match are called with a ? behind the maiin quantifier.

Further reading

#programming/regex