What's the default regex engine in C++11?

Viewed 116

Having seen a regex from one of my colleagues of <.*?> for stripping HTML tags, it didn't make sense to me to have zero-or-one occurrences (?) of zero-or-more characters (.*). Additionally, it appeared that it wouldn't work since.* is greedy so would strip out data between tags as well.

But on testing this in an online regex, it appeared my colleague was correct in his assertion that it makes it non-greedy so I decided to go and check why.

In any case, that's just the background. Looking at the C++11 standard, I finally found out that there were multiple possible regex engines that were allowed, as per 28.5.2 and table 128, and that:

A valid value of type syntax_option_type shall have exactly one of the elements ECMAScript, basic, extended, awk, grep, egrep, set.

However, that same section shows that the default used if not specified, match_default, is defined as zero, which clearly does not have one of the bits set.

Nowhere in the standard can I find where the default engine is specified so I'm wondering whether that's implementation defined or whether I've just missed it.

1 Answers

Okay, based on a comment, I looked more deeply into the standard and what the class hierarchy is.

I believe this is covered by the basic_regex constructors, in C++11 28.8.2, which are used by the higher levels. All of those constructors, such as:

basic_regex(const charT* p, flag_type f = regex_constants::ECMAScript);

use ECMAScript as the default engine.


And, for what it's worth, the ECMAScript syntax explicitly calls out the .*? behaviour that sent me on this quest in the first place. The ? is treated differently (i.e., is not the zero-or-one quantifier) when it immediately follows a quantifier (*, +, ?, {exact}, {min,} and {min,max}) in that it makes the matching non-greedy:

By default, all these quantifiers are greedy (i.e., they take as many characters that meet the condition as possible). This behavior can be overridden to ungreedy (i.e., take as few characters that meet the condition as possible) by adding a question mark, ?, after the quantifier.

For example, matching "(a+).*" against "aardvark" succeeds and yields aa as the first submatch, while matching "(a+?).*" against it also succeeds, but yields a as the first submatch.

Related