
17 Feb
2006
17 Feb
'06
10:33 a.m.
how can I implement regex_replace, that it only replaces a given numbers of occurencies?
You more or less have to do what regex_replace does internally: construct a regex_iterator and when each match is found: 1) Copy the last prefix to the output string (everything that wasn't matched since the previous match). 2) Call match_results<>::format to create the replacement text and append it to the output. 3) move to the next match if there is one and go back to (1). and finally: when you've found all the matches, don't forget to copy everything from the end of the last match to the end of the text to output. You'll find exactly that that code in boost/regex/v4/regex_replace.hpp, and it's commendably short too :-) John.