简体   繁体   English

JavaScript正则表达式在C ++中不匹配

[英]JavaScript regex will not match in C++

I've created and tested a JavasScript regex with regex101. 我已经使用regex101创建并测试了JavasScript regex。 After translating it into c++ (visual studio 2013) std::regex_search() returns false. 将其转换为C ++(Visual Studio 2013)之后,std :: regex_search()返回false。

What's my mistake? 我怎么了

Working JavaScript regex: 工作的JavaScript正则表达式:

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

Failing C++ regex (with same string): C ++正则表达式失败(具有相同的字符串):

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);

After πάντα ῥεῖ pointed out that this failure is caused by a bug in Visual Studio 2013, I switched to the boost regex library that provide the same functionality as STL. 当πάνταῥεῖ指出此故障是由Visual Studio 2013中的错误引起的之后,我切换到了提供与STL相同功能的boost regex库。 Just a change of the namespace from std to boost was required to fix the problem. 只需将名称空间从std更改为boost即可解决此问题。

What's my mistake? 我怎么了

It's not your mistake. 这不是你的错。 VS2013 std::regex implementation seems to be buggy. VS2013 std::regex实现似乎有问题。 Here is a reference: 这里是参考:

As others mentioned in comments, your regular expression works fine in newer versions of Visual-Studio or GCC. 正如其他人在评论中提到的那样,您的正则表达式在Visual Studio或GCC的较新版本中可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM