The little dirty secret is that all regex replace functions of all
languages maintain an internal string by which the output is constructed
from scratch.
The output is a catenation of between-match substrings plus the matched
string in formatted form.
The new string is then returned to the caller.
But, what if you want to supply a callback function to do your own
formatting that requires language constructs ?
In all of regex land, it's easy to simulate this by just sitting in a
regex_search loop and constructing your new output inside there,
based on each match.
Well, as far as C++(>=11) is concerned, _you can't provide a callback to
do this automatically !!
Pretty sad huh..
(Boost::Regex has this built into their regex replace function as
and option (callback functor).)
So, what do you do?
You have to roll your own general regex_replace() class that takes a
callback function, which does nothing more that what they all do as described.
Lucky for you someone has already done this using all the bells and whistles
of C++.
regex replace with callback in c++11?
Enjoy !!