I have a pattern '"XYZ\d\d' and a 'largish' string where this pattern can occur many times.
My objective is to find all instances of the pattern in the string and then to replace all the characters in that match with the letter 'A' in the original string.
I've so far got the following however there's an error:
#include <iostream> #include <regex> int main() { std::regex exp("XYZ\\d\\d"); std::smatch res; std::string str = " XYZ111 d-dxxxxxxx XYZ222 t-nyyyyyyyyy XYZ333 t-r "; auto itr = str.cbegin(); while (std::regex_search(itr, str.cend(), res, exp)) { std::cout << "[" << res[0] << "]" << std::endl; for (auto j = res[0].first; j != res[0].second; ++j) { *j = 'A'; // Error as dereferencing j causes a const reference } itr += res.position() + res.length(); } std::cout << std::endl; std::cout << "mod: " << str << std::endl; return 0; } I'm not sure what the correct process is when using C++11 regex facilities to accomplish my task.
Also was wondering is there something like regex_replace that takes a functor where one can specify how they'd like to change the match on every match occurring?
"XYZ"and then check whether it's followed by two digits. Using a regular expression seems like overkill for such a simple match.