Hide Forgot
Created attachment 511172 [details] Program demonstrating the problem Description of problem: When compiling a test program using C++ new <regex> header and regex_match or regex_search in combination with a regex using caret does not (never?) match as expected. Comparing to C's <regex.h> shows that on extended expressions the behavior is different, while it should not be. Version-Release number of selected component (if applicable): libstdc++-4.6.0-9.fc15.x86_64 How reproducible: Always Steps to Reproduce: 1. Compile attached example program with "gcc -o regex-cpp -std=c++0x -lstdc++ regex.cpp" 2. Run with "./regex-cpp '^foo.*' 'foobar'" Actual results: C++ regex does not match, while C does. Expected results: Both should match. Additional info: When run with regex 'foo.*' it matches as expected. Using regex_search yields the same result.
Can someone at least confirm this, or point out if there is an error in how I used it?
Benjamin, could you please look at this? Thanks.
Confirmed. I don't see an error in usage, this is a bug in libstdc++'s <regex>. I've opened up an upstream bug report here: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49870 As a temporary work-around, you can use boost's regex, which has the same interface but gives the correct result. Here's your example with that component: #include <boost/regex.hpp> #include <regex> #include <cstdio> // compile like // g++ -std=gnu++0x -g -O2 71871-regex.cpp -lboost_regex -Wfatal-errors // execute like // ./a.out "foo.*" "foobar" // ./a.out "^foo.*" "foobar" int main(int argc, char **argv) { // 1 std { std::regex expr(argv[1], std::regex_constants::extended); std::string test_string = argv[2]; printf("Applying regex '%s' to string '%s'\n", argv[1], argv[2]); if (std::regex_match(test_string, expr)) printf("C++: Match\n"); else printf("C++: NO match\n"); } // 2 boost { boost::regex expr(argv[1], boost::regex_constants::extended); std::string test_string = argv[2]; printf("Applying regex '%s' to string '%s'\n", argv[1], argv[2]); if (boost::regex_match(test_string, expr)) printf("C++: Match\n"); else printf("C++: NO match\n"); } return 0; }
Tracked upstream.