报错
在使用C++中的regex(正则表达式)时,发现编译能过,但是在运行到这块代码的时候,报下列错误,。
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted
解决方案:
1. 确定gcc版本
gcc版本在4.9+才支持正则表达式。
2. 确定添加了头文件
#include <regex>
测试代码
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
string line = "GET /404.html HTTP/1.1";
std::regex patten("^([^ ]*) ([^ ]*) HTTP/([^ ]*)$);
std::smatch subMatch;
if(regex_match(line,subMatch,patten)){
cout << subMatch[1] << endl; // GET
cout << subMatch[2] << endl; // /404.html
cout << subMatch[3] << endl; // 1.1
}
return 0;
}