gpt4 book ai didi

c++ - 使用 spirit 解析器从字符串中提取值

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:09:09 25 4
gpt4 key购买 nike

我有以下行/90pv-RKSJ-UCS2C usecmap

std::string const line = "/90pv-RKSJ-UCS2C usecmap";

auto first = line.begin(), last = line.end();

std::string label, token;
bool ok = qi::phrase_parse(
first, last,
qi::lexeme [ "/" >> +~qi::char_(" ") ] >> ' ' >> qi::lexeme[+~qi::char_(' ')] , qi::space, label, token);


if (ok)
std::cout << "Parse success: label='" << label << "', token='" << token << "'\n";
else
std::cout << "Parse failed\n";

if (first!=last)
std::cout << "Remaining unparsed input: '" << std::string(first, last) << "'\n";

我想在标签中90pv-RKSJ-UCS2C,在 token 变量中usecmap

我提取了 90pv-RKSJ-UCS2C 值但没有使用 cmap

最佳答案

使用 space 作为 skipper ,你永远无法匹配 ' '(它被跳过了!)。另请参阅:Boost spirit skipper issues

所以,要么不使用 skipper ,要么让 skipper 吃掉它:

bool ok = qi::phrase_parse(
first, last,
qi::lexeme [ "/" >> +qi::graph ] >> qi::lexeme[+qi::graph], qi::blank, label, token);

注意事项:

  • 我使用了 qi::graph 而不是 ~qi::char_("") 公式
  • 我使用blank_type 因为你说

    i have following line

    这意味着不应跳过行尾

演示

Live On Coliru

#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

int main()
{
std::string const line = "/90pv-rksj-ucs2c usecmap";

auto first = line.begin(), last = line.end();

std::string label, token;
bool ok = qi::phrase_parse(
first, last,
qi::lexeme [ "/" >> +qi::graph ] >> qi::lexeme[+qi::graph], qi::blank, label, token);

if (ok)
std::cout << "parse success: label='" << label << "', token='" << token << "'\n";
else
std::cout << "parse failed\n";

if (first!=last)
std::cout << "remaining unparsed input: '" << std::string(first, last) << "'\n";
}

打印:

parse success: label='90pv-rksj-ucs2c', token='usecmap'

关于c++ - 使用 spirit 解析器从字符串中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com