I am having ridiculous difficulties matching a regex and replacing the match with another string. I want to achieve this with iterators, as outlined below. The part that does not work is getting iterators that delimit the match in the original string and that I could then pass to string::replace. I tried using a std::match_results object to get a pair of iterators, but replacing m
by mr
in the call of regex_search fails.
I have the vague feeling that I either use the wrong match class or the wrong type of iterator, but somehow can't find my way out of template jungle.
std::string txt{ "aaa bbb" };
std::smatch m;
std::regex rx(R"(aaa)");
std::match_results<std::string::iterator> mr;
if (std::regex_search(cbegin(txt), cend(txt), m, rx)) {
std::cerr << m[0] << std::endl;
// what I need here are iterators that I can pass
// to string::replace
// txt.replace(i1 ,i2, std::string("ccc"));
}