I'm a piece of text"; std::regex rx("([^<]+)"); std::regex...">

Regex_search throws std :: regex_error

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"; 

Should this simple piece of code work? right? Apparently this is not the case:

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

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

+4
source share
1 answer

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 was fixed back in version 4.7.0

+3
source

All Articles