我在使用以下代码时遇到问题:
std::cmatch what;
std::regex regExp("(\\[-?\\d+\\])+", std::regex_constants::ECMAScript);
std::cout << "std::regex1: " << std::regex_match("[-123]", what, regExp);
std::cout << "std::regex2: " << std::regex_match("[-123][456]", what, regExp);
boost::cmatch what2;
boost::regex regExp2("(\\[-?\\d+\\])+", boost::regex_constants::ECMAScript);
std::cout << "boost::regex1: " << boost::regex_match("[-123]", what2, regExp2);
std::cout << "boost::regex2: " << boost::regex_match("[-123][456]", what2, regExp2);
我期望第二个表达式(“std::regex2”)返回 true,但它没有。 boost 中的相同表达式返回 true。
我玩了一下,发现它匹配 [123][-456] 或 [123][456][-789]。 “-”只接受最后一个元素,尽管它对所有元素都是可选的。 这是错误还是我做错了什么?
最好的问候, 马库斯
P.S.:我使用的是 VS2010。
最佳答案
我同意你的观点,“std::regex2”表达式应该返回 true。我也能够在 Visual Studio 2010 中验证这一点并重现了同样的问题。
所以我在 Visual Studio 2012 中尝试了相同的代码,这次您的代码产生了预期的答案 (true)。所以答案是这在 VS2010 中被破坏并在 VS2012 中修复。
Visual Studio 2012 中修复了几个正则表达式错误,因此这应该可以解释 VS2010 和 VS2012 之间的区别。有关 VS2012 中已修复的 STL 错误列表,请参阅下页。
http://blogs.msdn.com/b/vcblog/archive/2012/06/15/10320846.aspx
关于c++ - VS2010 的 std::regex_match 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21424743/