Quantifiers
Quantifiers are used to repeat part of regular expression. A quantifier influence the previous character or group (see grouping. The generic quantifier is {m,n} Quantifiers are used to repeat part of regular expression where m is the minimum number of repetition and n the maximum.
For example, /a{2,4}/ matches aa# or aaa or aaaa.
For convenience, all engine also define some special quantifier:
Shorthand | Equivalent to | Remarks |
---|---|---|
? | {0,1} | Optional item |
+ | {1,infinity} | Match at least once, and as many times as possible |
* | {0,infinity} | Match as many times as possible or not at all |
Greedy quantifiers
{n,m}, ?, + and * are known as greedy quantifier, which means that they will match as much as it can. For example, the regex /.*\/.*/ applied to usr/local/bin will match usr/local with its the first .* and bin with the second. Here, if you have not use capturing there is no special impact, but with /(.*)\/(.*)/ it will matter a lot. To understand how this works in a NFA engine and what .* really means, read more on backtracking.
Non-greedy or lazy quantifiers
{n,m}?, ??, +? and *? are the equivalent non-greedy quantifier to the previous one. Lazy quantifiers will match just the bare minimum to satisfy a match. Taking the similar example as in the previous paragraph, /(.*?)/(.*)/ applied to usr/local/bin will matches usr for the non-greedy quantifier .*?.
Possessive quantifiers
To really understand possessive quantifiers, you have to read regular expression backtracking. The possessive quantifiers are {n,m}+, ?+, ++ and *+. There are not available in NFA engines and may also be replaced by atomic grouping.