Syntax C ++ std :: regex

I cannot get the right rule to work correctly. In multi-line text in ECMAScript, is this a regular expression begin\n([\s\S]*\nend)? matches exactly what I need and I tested it here .

When I translated it into C ++, it does not correspond to the same text.

Here is my code in Visual C ++ 2010:

 #include <iostream> #include <regex> int main(int argc, char *argv[]) { std::regex metadataBlockRegex("begin\\n([\\s\\S]*\\nend)?", std::regex::ECMAScript); std::string text = "begin\n" " 123\n" "end\n"; std::sregex_iterator blocksBegin(text.begin(), text.end(), metadataBlockRegex); std::sregex_iterator blocksEnd; for (auto blockMatch = blocksBegin; blockMatch != blocksEnd; ++blockMatch) { std::cout << (*blockMatch)[0].str(); } return 0; } 

This only outputs the β€œbeginning”, and I expected that it would fit the entire text.

My question is: what is wrong here, and where can I find a detailed description of the std::regex engine syntax and how they handle multi-line strings.

+6
source share
2 answers

LWG 2503 added a multiline syntax parameter that should make your program work as expected when you use this option (for C ++ implementations that support this new feature).

LWG 2343 has a few more prerequisites that explain that ECMAScript RegExp objects have the Multiline property, which defaults to false, and the behavior is different with C ++ regex implementations.

Original answer from 2012:

what is wrong here.

Not sure it looks fine, but only the implementation of C ++ 11, to which I have access, do not support the <regex>

where can I find a detailed description of the syntax of the std :: regex engines and how they handle multi-line strings.

You cannot, as far as I know. The best place to look is probably the documentation for Boost.Regex , but keep in mind that it was promoted as it was proposed for standardization and has some features missing from std::regex .

-1
source

There is no multi-line support anyway ... not in MSVC10.

You need to fake a multiline with \ r and \ n in your templates. This is a big bummer.

+5
source

Source: https://habr.com/ru/post/918567/


All Articles