...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::xpressive::let — For binding local variables to placeholders in semantic actions when constructing a regex_iterator
or a regex_token_iterator
.
// In header: <boost/xpressive/regex_actions.hpp> template<typename... ArgBindings> unspecified let(ArgBindings const &... args);
xpressive::let()
serves the same purpose as match_results::let()
; that is, it binds a placeholder to a local value. The purpose is to allow a regex with semantic actions to be defined that refers to objects that do not yet exist. Rather than referring directly to an object, a semantic action can refer to a placeholder, and the value of the placeholder can be specified later with a let expression. The let expression created with let()
is passed to the constructor of either
or regex_iterator
.regex_token_iterator
See the section "Referring to Non-Local Variables" in the Users' Guide for more discussion.
Example:@code // Define a placeholder for a map object: placeholder<std::map<std::string, int> > _map;
// Match a word and an integer, separated by =>, // and then stuff the result into a std::map<> sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) ) [ _map[s1] = as<int>(s2) ];
// The string to parse std::string str("aaa=>1 bbb=>23 ccc=>456");
// Here is the actual map to fill in: std::map<std::string, int> result;
// Create a regex_iterator
to find all the matches sregex_iterator it(str.begin(), str.end(), pair, let(_map=result)); sregex_iterator end;
// step through all the matches, and fill in // the result map while(it != end) ++it;
std::cout << result["aaa"] << '
'; std::cout << result["bbb"] << '
'; std::cout << result["ccc"] << '
'; \endcodeThe above code displays:
1 23 456
Parameters: |
|