$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: shiwei xu (xushiweizh_at_[hidden])
Date: 2008-05-20 15:16:56
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;
}
///////////////////////////////////////////////////////////////////////////////