Boost.Xpressive で対応した文字で一括置換

少し前にコメントで教えていただいたので覚書。

[ソース]

#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>
#include <map>


int
main(){
    namespace x = boost::xpressive;
    using x::_;
    std::map<std::string, std::string> m = {
        { "(", "[ "  },
        { ")", " ]"  },
    };
    
    x::local<std::string const*> pstr;
    x::sregex const regex = (x::a1 = m)[pstr = &x::a1];

    std::string source = "(homu)(mado)(mami)";
    auto result = x::regex_replace(source, regex, *pstr);
    std::cout << result << std::endl;
    
    return 0;
}

[出力]

[ homu ][ mado ][ mami ]

コード自体は写経なんですが、いったいどういう挙動なのか…。
Boost.Xpressive わからん。

[boost]

  • ver 1.49.0