Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2008-03-16 18:44:47


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.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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