
On Sun, Mar 16, 2008 at 5:44 PM, Eric Niebler <eric@boost-consulting.com> wrote:
Eric Niebler wrote:
The formatter can be a function object, or even just a plain function. So, for instance, you can do this:
map<string,string> replacements;
string my_format(smatch const &what) { return replacements[what[0].str()]; }
int main() { string input = ...; sregex rx = ...; string output = regex_replace(input, rx, my_format); }
I've made a small addition ... the formatter can be a lambda, too, if you #include <boost/xpressive/regex_actions.hpp>. The above can now be written simply as:
using xpressive::ref; string output = regex_replace(input, rx, ref(replacements)[_]);
Here, "_" gets substituted with a sub_match representing the current match. You can also use s1, s2, etc., to access the other sub-matches.
How is "[_]" legal C++ syntax?