I can't seem to get my regex working right. On a multiline text in ECMAScript this regular expression begin\n([\s\S]*\nend)?
matches exactly what I need, and I tested it here.
When I translate it into C++, it fails to match 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 outputs only "begin" and I expected it to match the whole text.
My question is: what is wrong here and where can I find a detailed description of std::regex
engines' syntax and how they handle multiline strings.
No multiline support, anyway... not in MSVC10.
You need to fake multiline with \r & \n in your patterns. It's a major bummer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With