Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2006-02-17 05:33:28


> 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.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk