I've an python regex :
\A\s* # optional whitespace at the start, then
(?P<sign>[-+]?) # an optional sign, then
(?=\d|\.\d) # lookahead for digit or .digit
(?P<num>\d*) # numerator (possibly empty)
(?: # followed by
(?:/(?P<denom>\d+))? # an optional denominator
| # or
(?:\.(?P<decimal>\d*))? # an optional fractional part
(?:E(?P<exp>[-+]?\d+))? # and optional exponent
)
\s*\Z # and optional whitespace to finish
In other words, get named groups for :
But i'm confused with the C++11 regex format? As i've read there's few format supported, but I get an regex parser exception with this one. More, i've read that the named group is unsupported with C++11 regex.
How to have an C++11 compatible regex that provide equivalent scheme?
Thank you very much for your help.
You cannot preserve the named capturing groups, but you can use a multiline string literal to define the pattern in a verbose way:
std::string pat = "^\\s*" // optional whitespace at the start, then
"([-+]?)" // an optional sign, then
"(?=\\.?\\d)" // lookahead for digit or .digit
"(\\d*)" // numerator (possibly empty)
"(?:" // followed by
"(?:/(\\d+))?" // an optional denominator
"|" // or
"(?:\\.(\\d*))?" // an optional fractional part
"(?:E([-+]?\\d+))?" // and optional exponent
")"
"\\s*$"; // and optional whitespace to finish
std::regex e(pat);
std::string s(" -23/34 ");
std::smatch a;
if (std::regex_search(s, a, e))
std::cout << a[0] << endl;
See the C++ demo