C ++ regular expression doesn't understand
The following exits are "> Hut," where I expect it to display "Hut." I know it. * Greedy, but> needs to be matched, and it is outside the capture group, so why does this happen in my recharge?
#include <string> #include <regex> #include <iostream> using namespace std; int main() { regex my_r(".*>(.*)"); string temp(R"~(cols="64">Hut)~"); smatch m; if (regex_match(temp, m, my_r)) { cout << m[1] << endl; } } This is a bug in the implementation of libstdc ++. Look at them:
#include <string> #include <regex> #include <boost/regex.hpp> #include <iostream> int main() { { using namespace std; regex my_r("(.*)(6)(.*)"); smatch m; if (regex_match(std::string{"123456789"}, m, my_r)) { std::cout << m.length(1) << ", " << m.length(2) << ", " << m.length(3) << std::endl; } } { using namespace boost; regex my_r("(.*)(6)(.*)"); smatch m; if (regex_match(std::string{"123456789"}, m, my_r)) { std::cout << m.length(1) << ", " << m.length(2) << ", " << m.length(3) << std::endl; } } return 0; } If you compile gcc, the first (libstdc ++) returns a completely incorrect result of 9, -2, 4 , and the second (boost) returns 5, 1, 3 , as expected.
If you compile with clang + libC ++, your code works fine.
(Note that the regex implementation of libstdC ++ is “partially supported”, as described in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52719 .)
You can change your regular expression so that the matched parts are divided into groups:
std::regex my_r("(.*)>(.*)\\).*"); // group1>group2).* std::string temp("~(cols=\"64\">Hut)~"); std::sregex_iterator reg_it(temp.begin(), temp.end(), my_r); if (reg_it->size() > 1) { std::cout << "1: " << reg_it->str(1) << std::endl // group1 match << "2: " << reg_it->str(2) << std::endl; // group2 match } outputs:
1: ~(cols="64" 2: Hut Note that groups are specified using bracets ( /* your regex here */ ) , and if you want to make part of the curly brace of your expression, you need to avoid it with \ , which \\ in the code. For more information, see Grouping Constructs .
This question may also help you: How do I punch results from std :: regex_search?
Also do not use using namespace std; at the beginning of your files, this is bad practice.