[xpressive] How can I get nested match_results?

My program is the following. And my question is: I can get match_result such as item[time], item[count]. But, how can I get nested match_results, eg. item[time][year], item[time][month]? Best Regards, ShiWei Xu ---- #include <iostream> #include <boost/xpressive/xpressive.hpp> using namespace boost::xpressive; typedef std::string::const_iterator iterator_; typedef basic_regex<iterator_> regex_; typedef regex_iterator<iterator_> regex_iterator_; typedef regex_iterator_::value_type match_results_; typedef match_results_::value_type sub_match_; boost::proto::terminal<char>::type const _dot = {'.'}; boost::proto::terminal<char>::type const _gt = {'>'}; boost::proto::terminal<char>::type const _quot = {'\"'}; inline void example() { std::string str = "<tr><td>2008-05-20</td><td>70</td></tr>"; // define some custom mark_tags with names more meaningful than s1, s2, etc. mark_tag day(1), month(2), year(3), delim(4); regex_ rTime = // DateTime value (year = repeat<4>(_d)) >> // year (delim = (set='/','-')) >> // followed by a delimiter ... (month = repeat<1,2>(_d)) >> delim >> // month (day = repeat<1,2>(_d)) // day ; regex_ rInteger = +_d; // Integer value regex_ rHtmlProperties = +(+_w >> *_s >> '=' >> *_s >> _quot >> *~_quot >> _quot >> *_s); mark_tag time(1), count(2); regex_ rTrade = (("<tr" >> +_s >> rHtmlProperties >> _gt) | "<tr>") >> *_s >> "<td>" >> (time = rTime) >> "</td>" >> *_s >> "<td>" >> (count = rInteger) >> "</td>" >> *_s >> "</tr>" ; iterator_ first = str.begin(); iterator_ last = str.end(); regex_iterator_ cur(first, last, rTrade); regex_iterator_ end; for( ; cur != end; ++cur ) { match_results_ item = *cur; sub_match_ matched = item[0]; std::cout << matched << '\n'; std::cout << item[time] << ' ' << item[count] << '\n'; } } /////////////////////////////////////////////////////////////////////////////// // main int main() { example(); return 0; } ///////////////////////////////////////////////////////////////////////////////

You could use regex_id() to get the correct nested match_results. For example, to output the year: std::cout << item(rTime.regex_id())[year] << '\n'; Regards, Dave Jenkins "shiwei xu" <xushiweizh@gmail.com> wrote in message news:c01ea5030805201216h1a4a39a9xd35397b30d7e2a08@mail.gmail.com...
My program is the following. And my question is:
I can get match_result such as item[time], item[count]. But, how can I get nested match_results, eg. item[time][year], item[time][month]?
Best Regards,
ShiWei Xu
----
#include <iostream> #include <boost/xpressive/xpressive.hpp>
using namespace boost::xpressive;
typedef std::string::const_iterator iterator_; typedef basic_regex<iterator_> regex_; typedef regex_iterator<iterator_> regex_iterator_; typedef regex_iterator_::value_type match_results_; typedef match_results_::value_type sub_match_;
boost::proto::terminal<char>::type const _dot = {'.'}; boost::proto::terminal<char>::type const _gt = {'>'}; boost::proto::terminal<char>::type const _quot = {'\"'};
inline void example() { std::string str = "<tr><td>2008-05-20</td><td>70</td></tr>";
// define some custom mark_tags with names more meaningful than s1, s2, etc. mark_tag day(1), month(2), year(3), delim(4);
regex_ rTime = // DateTime value (year = repeat<4>(_d)) >> // year (delim = (set='/','-')) >> // followed by a delimiter ... (month = repeat<1,2>(_d)) >> delim >> // month (day = repeat<1,2>(_d)) // day ;
regex_ rInteger = +_d; // Integer value
regex_ rHtmlProperties = +(+_w >> *_s >> '=' >> *_s >> _quot >> *~_quot >> _quot >> *_s);
mark_tag time(1), count(2); regex_ rTrade = (("<tr" >> +_s >> rHtmlProperties >> _gt) | "<tr>") >> *_s >> "<td>" >> (time = rTime) >> "</td>" >> *_s >> "<td>" >> (count = rInteger) >> "</td>" >> *_s >> "</tr>" ;
iterator_ first = str.begin(); iterator_ last = str.end();
regex_iterator_ cur(first, last, rTrade); regex_iterator_ end;
for( ; cur != end; ++cur ) { match_results_ item = *cur; sub_match_ matched = item[0]; std::cout << matched << '\n'; std::cout << item[time] << ' ' << item[count] << '\n'; } }
/////////////////////////////////////////////////////////////////////////////// // main
int main() { example(); return 0; }
/////////////////////////////////////////////////////////////////////////////// _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (2)
-
Dave Jenkins
-
shiwei xu