daydayup
daydayup

Reputation: 2317

c++ regex abort error

I just started using c++ expression and couldn't get rid of this error. I am also confused why in some c++ regex examples, people use \\s \\{ \\w instead of just \s \{ \w?

#include<iostream>
#include<regex>
using namespace std;
int main()
{
        string s1("{1,2,3}");
        string s2("{}");
        regex e("\\{(\\d,?)+\\}", regex_constants::extended);
        if (regex_match(s1,e))
        cout << "yes" << endl;
}

Error info:

terminate called after throwing an instance of 'std::regex_error'
  what():  regex_error
Aborted (core dumped)

Edit: I answer @Lightness Races in Orbit's question here regarding the complier:

dpkg --list | grep compiler
ii  g++                                                   4:4.8.2-1ubuntu6                                    amd64        GNU C++ compiler
ii  g++-4.8                                               4.8.4-2ubuntu1~14.04                                amd64        GNU C++ compiler
ii  gcc                                                   4:4.8.2-1ubuntu6                                    amd64        GNU C compiler
ii  gcc-4.8                                               4.8.4-2ubuntu1~14.04                                amd64        GNU C compiler
ii  gfortran                                              4:4.8.2-1ubuntu6                                    amd64        GNU Fortran 95 compiler
ii  gfortran-4.8                                          4.8.4-2ubuntu1~14.04                                amd64        GNU Fortran compiler
ii  hardening-includes                                    2.5ubuntu2.1                                        all          Makefile for enabling compiler flags for security hardening
ii  libllvm3.4:amd64                                      1:3.4-1ubuntu3                                      amd64        Modular compiler and toolchain technologies, runtime library
ii  libplexus-archiver-java                               1.2-1                                               all          Archiver plugin for the Plexus compiler system
ii  libprotoc8:amd64                                      2.5.0-9ubuntu1                                      amd64        protocol buffers compiler library
ii  libxkbcommon-dev                                      0.4.1-0ubuntu1                                      amd64        library interface to the XKB compiler - development files
ii  libxkbcommon0:amd64                                   0.4.1-0ubuntu1                                      amd64        library interface to the XKB compiler - shared library
ii  protobuf-c-compiler                                   0.15-1build1                                        amd64        protocol buffers C compiler

Upvotes: 2

Views: 496

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

You've indicated you are using GCC 4.8. GCC 4.8 does not support <regex>, so that's not a good start.

However, beyond that, I can reproduce the problem too and I don't know why, sorry.

I am also confused why in some c++ regex examples, people use \s \{ \w instead of just \s { \w?

Consider this. When you want to stream a backslash to stdout, you have to write the backslash twice:

std::cout << "here's a backslash: \\" << std::endl;

It's no different when you want to send a backslash to the regex engine. The regex only has \s \{ \w, but you need to escape those backslashes for the string literal. You have two layers of "translation" going on here.

Upvotes: 2

Related Questions