【发布时间】:2012-04-09 18:45:50
【问题描述】:
知道为什么下面的代码会打印“no match”吗?与编译器或库版本有关的东西?我用 g++ a.cpp 编译。
#include <tr1/regex>
#include <iostream>
#include <string>
using namespace std;
int main()
{
const std::tr1::regex pattern("(\\w+day)");
std::string weekend = "Saturday and Sunday";
std::tr1::smatch result;
bool match = std::tr1::regex_search(weekend, result, pattern);
if(match)
{
for(size_t i = 1; i < result.size(); ++i)
{
std::cout << result[i] << std::endl;
}
}else
std::cout << "no match" << std::endl;
return 0;
}
【问题讨论】:
-
+1 表示简短的完整测试用例。
-
代码是如此明显,以至于我怀疑编译器(至少在我的版本中)对标准的支持不完全。我该如何验证?
-
仅供参考 - MSVC 2010 SP1 (16.00.40219.01) 编译并正确找到匹配项。您没有指定您使用的 GCC 版本。
-
在 g++ 4.6.1 和 g++ 4.3.4 中,此语句断言:
assert( std::tr1::regex_search("a", std::tr1::regex("a")) );