我只想匹配" {"。但是不知道为什么会出现这个错误:
terminate called after throwing an instance of 'std::regex_error' what(): regex_error Aborted (core dumped)
使用g ++版本4.6.3在Ubuntu上编译
g++ -std=c++0x a.c
#include<iostream>
#include<regex>
using namespace std;
main(int argc,char**argv){
if (regex_match("{1}" , std::regex ("[{]"))) {
cout<<"Hello World"<<endl;
}
}
我还查看了ECMAScript详细信息,这个正则表达式应该匹配。当我使用时,它也不匹配:std::regex ("\\{"))
我错了什么?
答案 0 :(得分:3)
你需要在least gcc 4.9让regexp与gcc一起使用,一旦你有4.9版本添加.*
以使其与字符串的其余部分匹配:
if (regex_match("{1}" , std::regex ("[{].*"))) {
^^
答案 1 :(得分:1)
我和你有同样的错误!我的IDE是 Clion ,
我选择的Clion的C ++版本是C ++ 17,我的测试代码是:
std::string pattern{ "http|hppts://\\w.*$" }; // url
std::regex re(pattern);
std::vector<std::string> str{ "http://blog.net/xxx",
"https://github.com/jasonhubs", "abcd://124.456",
"abcdhttps://github.com/jasonhubs"
};
for (auto tmp : str)
{
bool ret = std::regex_search(tmp, re);
if (ret) fprintf(stderr, "%s, can search\n", tmp.c_str());
else fprintf(stderr, "%s, can not search\n", tmp.c_str());
}
,我通过更新 gcc 和 g ++ 来解决。
sudo yum install centos-release-scl
sudo yum-config-manager --enable rhel-server-rhscl-7-rpms
sudo yum install devtoolset-7
scl enable devtoolset-7 bash
。