2

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
daydayup
  • 2,049
  • 5
  • 22
  • 47
  • What compiler are you using? – Lightness Races in Orbit Sep 12 '15 at 21:24
  • 1
    ([repro](http://coliru.stacked-crooked.com/a/b0edd9d6bc1ad8ac)) – Lightness Races in Orbit Sep 12 '15 at 21:26
  • 1
    The fact that you have to use `\\ ` instead of `\ ` all the time in regexes makes raw string literals a good fit for them: `R"(\{(\d,?)+\})"` (Aside: how are you supposed to enclose "\\" or "\" in backticks in a comment? It works intuitively in answer previews.) – chris Sep 12 '15 at 21:26
  • I am using g++. g++ --std c++11 code.cpp -o hello @LightnessRacesinOrbit – daydayup Sep 12 '15 at 21:31
  • @daydayup: That doesn't tell me what compiler you're using. There are hundreds of versions of compilers that can be invoked using the command `g++` (and, as I learnt today, on OSX that can even invoke Clang!). – Lightness Races in Orbit Sep 12 '15 at 21:33
  • @LightnessRacesinOrbit. I run dpkg --list | grep compiler in ubuntu and I added the output to the original question. Can you tell what compiler it is using? Otherwise can you tell me how to find out? I really don't know how to find out what compiler c++ is using on my machine. Thank you – daydayup Sep 12 '15 at 21:40
  • Okay, so GCC 4.8. That doesn't support regexes!!! No idea who "@Lightroudorbit" is, mind you. – Lightness Races in Orbit Sep 12 '15 at 21:41
  • For future reference, pass `--version` or `-v` to binaries to find out what they are. – Lightness Races in Orbit Sep 12 '15 at 21:43
  • Thank you! That's really helpful. Can I ask how should I change the compiler when compiling this code? – daydayup Sep 12 '15 at 21:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89485/discussion-between-daydayup-and-lightness-races-in-orbit). – daydayup Sep 12 '15 at 21:49

1 Answers1

2

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.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I tried with libc++ and its `what()` is a bit more informative: *An empty regex is not allowed in the POSIX grammar.* It seems extended POSIX must not include `\d`, but I thought I tried replacing that before. – chris Sep 12 '15 at 21:39