4
std::cmatch res;
std::string str = "<h2>I'm a piece of text</h2>";
std::regex rx("<h(.)>([^<]+)");
std::regex_search(str.c_str(), res, rx);
std::cout << res[1] << ". " << res[2] << "\n";

This simple piece of code should work? right? Apparently it doesn't:

terminate called after throwing an instance of 'std::regex_error'
  what():  regex_error
Aborted

Compiler( gcc 4.7.0 ) bug or am I missing something?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
user1233963
  • 1,450
  • 15
  • 41

1 Answers1

3

The brackets in the regex seem to be causing the problem. See this SO thread for more details and possible workarounds.

Also (from the same thread), gcc version 4.6.1 had only partial support for std::regex, I don't know if it has been fixed in version 4.7.0 yet

Community
  • 1
  • 1
Attila
  • 28,265
  • 3
  • 46
  • 55
  • 2
    It has not been fixed AFAICT: http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011 – rubenvb Jun 21 '12 at 14:10