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?

5
  • 5
    Remember that C++11 is quite new, and not all compilers support all features yet. Specifically, GCC do not support regex_searchyet. Commented Sep 5, 2012 at 10:57
  • Thanks for pointing that out. I guess I'll go look at Boost's regex library now. Commented Sep 5, 2012 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). Commented Sep 6, 2012 at 0: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. Commented Sep 6, 2012 at 8: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. Commented Sep 6, 2012 at 8:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.