|
Boost : |
From: Dick Bridges (dbridges_at_[hidden])
Date: 2006-01-02 19:57:02
I am trying to use Xpressive for parsing string-based message buffers.
The messages start with a fixed string and end with an "empty" line
(similar in form to HTTP). There is an example in the code below.
Following a successful search, I'd like to be able process messages
in the buffer with something like this pseudo code:
for each message
for each name_value pair
make_pair(name, value)
prepend what.suffix() to the next packet
The examples of repeating an embedded regex I found in the
.pdf and the examples subdirectory seemed close to what I need
but I have not been able to make them work.
Here is code that is the closest I've been able to get:
#include <iostream>
#include <boost/xpressive/xpressive.hpp>
using namespace std;
using namespace boost::xpressive;
int
main(int argc, char **argv) {
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";
sregex name_value_pair_ =
(+alnum >> *_s >> "=" >> *_s >> +_d >> *_s >> _ln);
sregex message_ =
(*_s >> "FROGGIE" >> _ln >> +name_value_pair_ >> _ln);
sregex re_ = +(s1= message_);
smatch what;
if( regex_search( buffer, what, re_ ) ) {
cout << "\n<what.size()>" << what.size() << "</what.size()>"
<< "\n<what[0]>\n" << what[0] << "</what[0]>"
<< "\n<what[1]>\n" << what[1] << "</what[1]>"
<< "\n<what[2]>\n" << what[2] << "</what[2]>"
<< "\n<what.suffix()>\n" << what.suffix() << "</what.suffix()>"
<< endl;
}
} // main
I am not very accomplished with regular expressions but what[0] and
what.suffix() look correct and I hoped that what.size() held the
number of messages and what[1] and what[2] accessed each in turn.
I clearly did not understand.
<what.size()>2</what.size()>
<what[0]>
FROGGIE
Volume = 1
Other1= 2
Channel=3
Other =4
FROGGIE
Volume = 5
Other1= 6
Channel=7
Other =8
</what[0]>
<what[1]>
FROGGIE
Volume = 5
Other1= 6
Channel=7
Other =8
</what[1]>
<what[2]>
</what[2]>
<what.suffix()>
FROGGIE
Volume = 9
Other1= 0
Channel=10
</what.suffix()>
OS is Linux Core 3 2.6.9-1.667. Compiler is gcc 3.4.4-linux.
Xpressive README.txt (in the .zip) says "xpressive 0.9.8d".
If someone could point me in the right direction, I'd surely
appreciate it.
Regards,
Dick Bridges
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk