【发布时间】:2012-08-20 22:36:39
【问题描述】:
让 C++ 正则表达式字符串捕获工作。我已经尝试过 Windows 与 Linux、Boost 与原生 C++ 0x11 的所有四种组合。示例代码为:
#include <string>
#include <iostream>
#include <boost/regex.hpp>
//#include <regex>
using namespace std;
using namespace boost;
int main(int argc, char** argv)
{
smatch sm1;
regex_search(string("abhelloworld.jpg"), sm1, regex("(.*)jpg"));
cout << sm1[1] << endl;
smatch sm2;
regex_search(string("hell.g"), sm2, regex("(.*)g"));
cout << sm2[1] << endl;
}
最接近的是 g++ (4.7) 和 Boost (1.51.0)。在那里,第一个 cout 输出预期的 abhelloworld.,但第二个 cout 没有输出。
使用 -std=gnu++11 和 <regex> 而不是 <boost/regex.hpp> 的 g++ 4.7 不会产生任何输出。
使用本机 <regex> 的 Visual Studio 2012 会产生关于不兼容字符串迭代器的异常。
带有 Boost 1.51.0 和 <boost/regex.hpp> 的 Visual Studio 2008 产生关于“标准 C++ 库无效参数”的异常。
这些是 C++ 正则表达式中的错误,还是我做错了什么?
【问题讨论】:
标签: c++ regex visual-studio boost g++