I simply want to match "{". But don't know why giving this error:
terminate called after throwing an instance of 'std::regex_error' what(): regex_error Aborted (core dumped)
Compilation on Ubuntu with g++ version 4.6.3
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;
}
}
I've also checked the ECMAScript details and this regular expression should match. It also does not match when I use like : std::regex ("\\{"))
What am I wrong?
regex_error in C++. regex_error is present inside the Header “regex” and inside the Class regex_error;. It helps us to know about the errors which are thrown during the program execution, it defines the type of the object of exception in regular expressions library, Also describes an error in the construction or use of the basic_regex object.
If it doesn't work, try removing the std::regex_constants::ECMAScript argument. If it does work, then you are compiling with a different compiler than the Packager is. If it does work, then you are compiling with a different compiler than the Packager is.
The complexity of an attempted match against a regular expression exceeded a pre-set level when contains an invalid character range. The expression contains invalid range between braces { and }. The expression contains mismatched braces { and }. The expression contains mismatched parentheses ( and ).
You need at least gcc 4.9 to make regexps work with gcc, once you will have 4.9 version add .*
to make it match the rest of the string:
if (regex_match("{1}" , std::regex ("[{].*"))) {
^^
http://coliru.stacked-crooked.com/a/99e405e66906804d
I have the same error with you! And my IDE is Clion,
I choose the C++ version with Clion is C++17 and my test code is:
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());
}
and I solve it by updating the gcc and g++.
sudo yum install centos-release-scl yum-utils
sudo yum-config-manager --enable rhel-server-rhscl-7-rpms
sudo yum install devtoolset-7
scl enable devtoolset-7 bash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With