2

The regex_search function isn't quite behaving as expected.

#include <iostream>
#include <regex>
#include <string>

using namespace std;

int main()
{
    string str = "Hello world";
    const regex rx("Hello");
    cout << regex_search(str.begin(), str.end(), rx) << endl;
    return 0;
}

The output is

0

What's going on?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
wenderen
  • 161
  • 2
  • 7
  • 5
    Remember that C++11 is quite new, and not all compilers support all features yet. Specifically, [GCC do not support `regex_search`yet](http://stackoverflow.com/a/11629428/440558). – Some programmer dude Sep 05 '12 at 10:57
  • Thanks for pointing that out. I guess I'll go look at Boost's regex library now. – wenderen Sep 05 '12 at 11:01
  • 1
    @wenderen : Be sure to look into Boost.Xpressive rather than Boost.Regex if you want to avoid having to build Boost (Xpressive is header-only unlike Regex). – ildjarn Sep 06 '12 at 00:45
  • @JoachimPileborg So what is *gcc*'s intention with this oddity? Why then expose the `std::regex_search` function **if it doesn't work** (and his example isn't really an edge case)? I'd rather miss this function than use it while it silently **just doesn't work**. – Christian Rau Sep 06 '12 at 08:36
  • @ChristianRau I don't know what the GCC designers think, but I'm guessing it's better to have a complete interface, even if some of the actual functionality behind that interface is missing. – Some programmer dude Sep 06 '12 at 08:40

1 Answers1

1

As pointed out in comments to the question, older implementations of the C++ standard libraries did not yet support all features in C++11. Of course, libc++ being an exception because it was originally built specifically for C++11.

According to this bug report support for <regex> in libstdc++ was only implemented for version 4.9 of GCC. You can check the current status on the libstdc++ status page.

One can confirm, that your example works with GCC 4.9 while still failing with GCC 4.8.

jotik
  • 17,044
  • 13
  • 58
  • 123