|
Boost : |
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.
>
> <snip>
>
> You are confusing a backreference with a nested regex. A backreference
> (s1, s2, etc.) is accessed positionally from the match results struct
> like what[1], what[2], etc.
>
> In contrast, when you nest a regex in another regex, you ended up with
> nested match results.
<snip>
>
> 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.
#include <string>
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/xpressive/xpressive.hpp>
using namespace std;
using namespace boost::xpressive;
int main()
{
std::string buffer =
"FROGGIE\r\n"
"Volume = 1\r\n"
"Other1= 2\r\n"
"Channel=3\r\n"
"Other =4\r\n"
"\r\n"
"FROGGIE\r\n"
"Volume = 5\r\n"
"Other1= 6\r\n"
"Channel=7\r\n"
"Other =8\r\n"
"\r\n"
"FROGGIE\r\n"
"Volume = 9\r\n"
"Other1= 0\r\n"
"Channel=10\r\n"
"\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_;
smatch what;
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[0] << 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;
}
}
}
return 0;
}
-- Eric Niebler Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk