A quantifier specifies how many instances of the previous element (which can be a character, a group, or a character class) must be present in the input string for a match to occur. Quantifiers include the language elements listed in the table 1.
Quantifier |
Description |
* |
Matches the previous element zero or more times. |
+ |
Matches the previous element one or more times. |
? |
Matches the previous element zero or one time. |
{ n } |
Matches the previous element exactly n times. |
{ n ,} |
Matches the previous element at least n times. |
{ n , m } |
Matches the previous element at least n times, but no more than m times. |
*? |
Matches the previous element zero or more times, but as few times as possible. |
+? |
Matches the previous element one or more times, but as few times as possible. |
?? |
Matches the previous element zero or one time, but as few times as possible. |
{ n }? |
Matches the preceding element exactly n times. |
{ n ,}? |
Matches the previous element at least n times, but as few times as possible. |
{ n , m }? |
Matches the previous element between n and m times, but as few times as possible. |
Table 1: Quantifiers
Pattern |
Matches |
\d*\.\d |
".0", "19.9", "219.9" |
"be+" |
"bee" in "been", "be" in "bent" |
"rai?n" |
"ran", "rain" |
",\d{3}" |
",043" in "1,043.6", ",876", ",543", and ",210" in "9,876,543,210" |
"\d{2,}" |
"166", "29", "1930" |
"\d{3,5}" |
"166", "17668" "19302" in "193024" |
\d*?\.\d |
".0", "19.9", "219.9" |
"be+?" |
"be" in "been", "be" in "bent" |
"rai??n" |
"ran", "rain" |
",\d{3}?" |
",043" in "1,043.6", ",876", ",543", and ",210" in "9,876,543,210" |
"\d{2,}?" |
"166", "29", "1930" |
"\d{3,5}?" |
"166", "17668" "193", "024" in "193024" |
Table 2: Examples for quantifiers