siskat siskat - 2 months ago 15
C++ Question

JavaScript regex will not match in C++

I've created and tested a JavasScript regex with regex101. After translating it into c++ (visual studio 2013) std::regex_search() returns false.

What's my mistake?

Working JavaScript regex:

struct[\s]+([\w]+).+?public[\s]+([\w]+)[\s\S]+?\{([\s\S]+?)\};


Failing C++ regex (with same string):

std::regex rgxGetStructs("struct[\\s]+([\\w]+).+?public[\\s]+([\\w]+)[\\s\\S]+?\\{([\\s\\S]+?)\\};", std::regex_constants::ECMAScript);
const char* test = "struct mySTruct : public boss { int test; };";
bool chk = std::regex_search(std::string(test), rgxGetStructs);

Answer

What's my mistake?

It's not your mistake. VS2013 std::regex implementation seems to be buggy. Here is a reference:

As others mentioned in comments, your regular expression works fine in newer versions of Visual-Studio or GCC.