Quantifiers

suggest change

Let’s say that we’re given const string input as a phone number to be validated. We could start by requiring a numeric input with a zero or more quantifier: regex_match(input, regex("\\d*")) or a one or more quantifier: regex_match(input, regex("\\d+")) But both of those really fall short if input contains an invalid numeric string like: “123” Let’s use a n or more quantifier to ensure that we’re getting at least 7 digits:

regex_match(input, regex("\\d{7,}"))

This will guarantee at least seven digits, but input could also contain a numeric string that’s too long like: “123456789012”. So let’s go with a between n and m quantifier so the input is at least 7 digits but not more than 11:

regex_match(input, regex("\\d{7,11}"));

This gets us closer, but illegal numeric strings that are in the range of [7, 11] are still accepted, like: “123456789”. The next pattern uses ? as “zero or one”, which is not the lazy suffix:

regex_match(input, regex("\\d?\\d{7,10}"))

\d? is greedy, and it means the same thing as \d{0,1}. The lazy form is \d??. The ? suffix, the lazy quantifier, can be appended to another quantifier and then matches as few characters as possible. On a full match, greedy \d? gives its digit back when keeping it would make \d{7,10} fail, so this pattern still accepts 7 to 11 digits. It does not check that a leading digit is a country code.

Now, how would we make the area code optional and only accept a country code if the area code was present?

regex_match(input, regex("(?:\\d{3,4})?\\d{7}"))

In this final regex, the \d{7} requires 7 digits. These 7 digits are optionally preceded by either 3 or 4 digits. The ? on the group is greedy, so the group prefers to match, and “1234567” still matches with the group empty. A missing area code is not rejected.

Note that we did not append the lazy quantifier: \d{3,4}?\d{7}. That \d{3,4}? prefers 3 characters and can expand to 4. It still requires those digits, so it is not an optional area code.


In conclusion of the quantifier topic, I’d like to mention the other appending quantifier that you can use, the possessive quantifier. Either the lazy quantifier or the possessive quantifier can be appended to any quantifier. The possessive quantifier‘s only function is to assist the regex engine by telling it, greedily take these characters and don’t ever give them up even if it causes the regex to fail. std::regex uses the ECMAScript grammar, which has no possessive quantifiers, so these patterns make the std::regex constructor throw std::regex_error. In an engine that does accept them, this does not make much sense: regex_match(input, regex("\\d{3,4}+\\d{7}")) Because an input like: “1234567890” wouldn’t be matched as \d{3,4}+ will always match 4 characters even if matching 3 would have allowed the regex to succeed. The possessive quantifier is best used when the quantified token limits the number of matchable characters. For example:

regex_match(input, regex("(?:.*\\d{3,4}+){3}"))

Can be used to match if input contained any of the following:

123 456 7890 123-456-7890 (123)456-7890 (123) 456 - 7890

But when this regex really shines is when input contains an illegal input:

12345 - 67890

Without the possessive quantifier the regex engine may go back and test other splits between .* and the 3 or 4 digits. The possessive form does not give those digits back. The leading .* can still backtrack. The story that matching stops at one particular character of an illegal input depends on the engine.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents