The following outputs ">Hut" where I expect it to output "Hut". I know that .* is greedy but > must be matched and it is outside of the capture group so why is it in my submatch?
#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 libstdc++'s implementation. Watch these:
#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 with gcc, the first one (libstdc++) returns the totally wrong result 9, -2, 4 and the second one (boost's implementation) returns 5, 1, 3 as expected.
If you compile with clang + libc++, your code works fine.
(Note that libstdc++'s regex implementation is only "partially supported", as described in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52719.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With