John Maddock wrote:
Having done something similar a number of times, I wonder if it would be worth implementing a variant of regex_replace where a functor is called that accepts the matched string and returns the replacement string.
I think this is a common usage scenario.
Me too: I was thinking exactly this as I wrote that reply, I'll try and add this for the next release - can I get you to file a feature request at svn.boost.org so I don't forget?
I don't think such a callback would be useful to the OP. If the callback merely accepts the matched string, it would need to reparse the string to find the sub-matches. In addition, accepting and returning strings imposes unnecessary dynamic allocations. Xpressive's regex_replace() algorithm can accept a formatter function or function object with one of three different signatures. It can either: a) Accept the match_results object, and return a std::string, or b) Accept the match_results object and an output iterator, and return the output iterator, or b) Accept the match_results object, an output iterator and the match flags, and return the output iterator. You can read about it here: http://tinyurl.com/bd6c3f. I like this interface because it gives the formatter object lots of context and makes formatting potentially very efficient. It would be great if Boost.Regex and Boost.Xpressive settled on a common callback interface for regex_replace(). -- Eric Niebler BoostPro Computing http://www.boostpro.com