3

I have the following executable. I compile it with gcc 4.7.2 (g++ foo.cc -std=c++11).

At run time, the exception regex_error is thrown.

What am I doing wrong ?

#include <regex>

int main(int, char**){
    std::regex re("\\d");
}

UPDATE The error code in the exception is error_escape. So I tried "\\d". It doesn't fail at runtime, but I doesn't match againt "1", but it DOES match "\d". So this is clearly not what I want

Tristram Gräbener
  • 9,601
  • 3
  • 34
  • 50

2 Answers2

2

So the answer seem to be that the implementation in the GCC4.7 STL is incomplete. Thank you all for your comments.

No matches with c++11 regex

Thank you soon and Nathan Ernst !

Community
  • 1
  • 1
Tristram Gräbener
  • 9,601
  • 3
  • 34
  • 50
-3

Why not catch it?

#include <regex>
#include <iostream>

int main(int, char**){
    try {
        std::regex re("\\d");
    } catch(std::exception const& e) {
        std::cout << e.what() << "\n";
    }
}
Alex Chamberlain
  • 4,147
  • 2
  • 22
  • 49
  • 1
    How does this explain the exception? – Lightness Races in Orbit Mar 22 '13 at 16:28
  • @LightnessRacesinOrbit In most cases, `e.what()` will give you a sensible explanation of what's going on... – Alex Chamberlain Mar 22 '13 at 16:32
  • The OP already knows which exception is being thrown. The question is _why_. – Lightness Races in Orbit Mar 22 '13 at 16:36
  • 1
    @LightnessRacesinOrbit http://en.cppreference.com/w/cpp/regex/regex_error suggests that it would give an explanation of what went wrong. – Alex Chamberlain Mar 22 '13 at 16:36
  • In this case `e.what()` returns `"regex_error"` which is not very helpful. If you catch `std::regex_error` explicitly, you can print out the return value of `e.code()` , which you can decipher by looking at the regex header files. However, in this case all of this is a wild goose chase because there is *nothing wrong with the regular expression* -- in this case, gcc 4.7 just has a very broken implementation of regex that fails almost everything. – Mark Lakata Jun 08 '16 at 17:49