I'm using std::regex_replace to modify the string. I need both to restrict substitutions made and to get number of them done.
I used following code:
std::wregex rx(pattern);
size_t n = 0; // one match
size_t ns = 3;
wstring result = src;
wstring old; // old value
bool replaced = false;
do {
old = result;
result = std::regex_replace(result, rx, replace, std::regex_constants::format_first_only);
replaced = result != old;
if (replaced)
n++;
} while (replaced && n < ns);
It works fine, I can both restrict substitutuins amount and get their number. But this code analyzes the string from its start, as a result, if I have following values:
"banana" for src, "(an)" for pattern and "$1-" for replace
it produces following output: ban---ana instead of ban-an-a. Obviously, this is because std::regex_replace analyzes string from start. A solution might be to use iterators to define first character to analyze, but in this case I need to get iterator that points to characters after those that were replcased, but how can I get it?
Aucun commentaire:
Enregistrer un commentaire