【发布时间】:2023-03-12 15:48:01
【问题描述】:
我需要使用 boost::spirit::qi: 解析如下字符串:
str1
str1_str
str1_str/str
str1_str/str/*
即需要解析由'/'分隔的标识符字符串,如果最后一个符号是'/',那么'*'应该在后面。
我写了下面的代码来完成这项工作:
#include <boost/spirit/include/qi.hpp>
#include <boost/algorithm/string/join.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace client
{
namespace qi = boost::spirit::qi;
template <typename Iterator>
bool parseName(Iterator first, Iterator last, std::string& name)
{
std::vector<std::string> vec;
char c;
boost::optional<std::string> o;
std::string spaces;
std::string spaces1;
bool r = qi::phrase_parse(first, last,
(
qi::alnum >> *(+qi::alnum | qi::string("_") | (qi::string("/") >> +qi::alnum)) >> -qi::string("/*")
)
,
qi::blank, c, vec, o);
if (first != last) // fail if we did not get a full match
return false;
name = c + boost::algorithm::join(vec, "");
if (o) {
name += *o;
}
return r;
}
}
int main()
{
std::string str;
std::getline(std::cin, str);
std::string name;
if (client::parseName(str.begin(), str.end(), name)) {
std::cout << "parsed:\n";
std::cout << "name: " << name << std::endl;
} else {
std::cout << "not oook\n" ;
}
return 0;
}
我想知道为什么qi::phrase_parse 不能将所有匹配项写入一个属性string 或至少vector<string>?或者我做错了什么。
如何修改上面的代码以将匹配的输出写入一个字符串而不传递 char 和 boost::optional<std::string> 属性?
提前致谢!
【问题讨论】:
标签: c++ boost boost-spirit boost-spirit-qi