Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

terminate called after throwing an instance of 'std::regex_error'

Tags:

c++

g++

ubuntu

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

Program

#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?

like image 446
user5858 Avatar asked May 24 '16 10:05

user5858


People also ask

What is regex_error in C++?

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.

Why does my regex not work in a package?

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.

When does the complexity of an attempted match against a regular expression?

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 ).


2 Answers

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

like image 67
marcinj Avatar answered Oct 11 '22 20:10

marcinj


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++.

  1. sudo yum install centos-release-scl yum-utils
  2. sudo yum-config-manager --enable rhel-server-rhscl-7-rpms
  3. sudo yum install devtoolset-7
  4. scl enable devtoolset-7 bash
like image 41
Jason.Z Avatar answered Oct 11 '22 19:10

Jason.Z