Reputation: 231
Given that this code works
std::string pattern = "(\\d+)";
std::regex iregex = std::regex(pattern, std::regex_constants::icase);
I am getting a error for bellow code
std::string pattern = "(\\d+)";
std::regex iregex = std::regex(pattern, std::regex_constants::extended);
can someone please explain the difference..??
Upvotes: 0
Views: 1200
Reputation: 1014
std::regex
can be used with or without flags (the second parameter).
If there is no flag modifying the grammar: (basic, extended, awk, grep, egrep), it will use by default ECMAScript.
So flags like (icase, nosubs, optimize, collate) by default use the ECMAScript grammar.
Now the 2 flags from the question in detail
From documentation it says that if you use std::regex_constants::extended
it will
Use the extended POSIX regular expression grammar grammar documentation.
And in the grammar documentation it says:
An ordinary character is an ERE that matches itself. An ordinary character is any character in the supported character set, except for the ERE special characters listed in ERE Special Characters. The interpretation of an ordinary character preceded by an unescaped ( '\' ) is undefined, except in the context of a bracket expression (see ERE Bracket Expression).
And as you can see, \d
is not in the special characters list.
As for the std::regex_constants::icase
:
std::regex(pattern, std::regex_constants::icase)
is equivalent with std::regex(pattern, std::regex::ECMAScript | std::regex::icase)
.
And the ECMAScript grammar accepts \d
, which you have to escape so \\d
. You can read more about this grammar here.
std::regex_constants::extended
doesn't know about \d
and accepts \\
only between brackets ([]
).
std::regex_constants::icase
accepts it because it uses the std::regex_constants::ECMAScript
grammar.
Upvotes: 1