I have problems with Boost.Spirit parsing a string.
The string looks like
name1 has this and that.\n
name 2 has this and that.\n
na me has this and that.\n
and I have to extract the names. The text "has this and that" is always the same but the name can consist of spaces therefore I can't use graph_p.
1) How do I parse such a string?
Since the string has several lines of that format I have to store the names in a vector.
I used something like
std::string name;
rule<> r = *graph_p[append(name)];
for saving one name but
2) what's the best way to save several names in a vector?
Thanks in advance
Konrad
I think this will do the trick:
vector<string> names;
string name;
parse(str,
*(
(*(anychar_p - "has this and that.")) [assign_a(name)]
>> "has this and that.\n") [push_back_a(names, name)]
))
If you use the newer Spirit V2.x (which is the default since Boost V1.42), this is as easy as:
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
std::vector<std::string> names;
std::string input = "name1 has this and that.\n"
"name 2 has this and that.\n"
"na me has this and that.\n";
bool result = qi::parse(
input.begin(), input.end(),
*(*(qi::char_ - " has this and that.\n") >> " has this and that.\n"),
names
);
After which, if result is true, the vector names will hold all parsed names (tested with Boost V1.45).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With