Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::regex multiline syntax

Tags:

c++

regex

c++11

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.

like image 723
Gart Avatar asked Jun 20 '12 14:06

Gart


1 Answers

No multiline support, anyway... not in MSVC10.

You need to fake multiline with \r & \n in your patterns. It's a major bummer.

like image 106
CodeAngry Avatar answered Nov 08 '22 10:11

CodeAngry