From: Eric Niebler (eric_at_[hidden])
Date: 2006-01-03 04:43:57
Eric Niebler wrote:
> Dick Bridges wrote:
>>I am trying to use Xpressive for parsing string-based message buffers.
> You are confusing a backreference with a nested regex. A backreference
> (s1, s2, etc.) is accessed positionally from the match results struct
> like what, what, etc.
> In contrast, when you nest a regex in another regex, you ended up with
> nested match results.
> You'll notice that I eliminated your name_value_pair_ regex. That's
> because it exposed a bug in xpressive. :-P
Fixed. Please get the latest version of xpressive from the Vault. Below
is a nice little program that will parse your messages and print out the
name/value pairs. It also is a nice little demo of backrefs and nested
results. I may use this in xpressive's docs, with your permission. I'll
definitely add this as a test case.
using namespace std;
using namespace boost::xpressive;
std::string buffer =
"Volume = 1\r\n"
"Volume = 5\r\n"
"Volume = 9\r\n"
mark_tag name(1), value(2);
sregex name_value_pair_ =
(name= +alnum) >> *_s >> "=" >> *_s >>
(value= +_d) >> *_s >> _ln;
sregex message_ =
*_s >> "FROGGIE" >> _ln >> +name_value_pair_ >> _ln;
sregex re_ = +message_;
if(regex_search(buffer, what, re_))
cout << "Msg count: " << what.nested_results().size() << endl;
BOOST_FOREACH(smatch const &msg, what.nested_results())
cout << "Message:\n" << msg << endl;
cout << " name/value pair count: "
<< msg.nested_results().size() << endl;
BOOST_FOREACH(smatch const &nvp, msg.nested_results())
cout << " name:" << nvp[name]
<< ", value:" << nvp[value] << endl;
-- Eric Niebler Boost Consulting www.boost-consulting.com