c++ - 使用boost spirit从括号中提取字符串

标签 c++ boost boost-spirit boost-spirit-qi

我有以下字符串:

%%DocumentNeededResources: CMap (90pv-RKSJ-UCS2C)

我想解析它并存储/提取括号中的 90pv-RKSJ-UCS2C 字符串。

我的规则如下:

std::string strLinesRecur = "%%DocumentNeededResources: CMap (90pv-RKSJ-UCS2C)";
std::string strStartTokenRecur;
std::string token_intRecur;
bool bParsedLine1 = qi::phrase_parse(strLinesRecur.begin(), strLinesRecur.end(), +char_>>+char_,':', token_intRecur, strStartTokenRecur);

最佳答案

您似乎认为 skipper 是分隔符。恰恰相反 ( Boost spirit skipper issues )。

在这种罕见的情况下,我认为我更喜欢正则表达式。但是,既然你问了这里的 spirit :

Live On Coliru

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

namespace qi = boost::spirit::qi;

int main() {
    std::string const line = "%%DocumentNeededResources: CMap (90pv-RKSJ-UCS2C)";

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

    std::string label, token;
    bool ok = qi::phrase_parse(
            first, last, 
            qi::lexeme [ "%%" >> +~qi::char_(":") ] >> ':' >> qi::lexeme["CMap"] >> '(' >> 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";
}

打印

Parse success: label='DocumentNeededResources', token='90pv-RKSJ-UCS2C'

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

相关文章:

c++ - 无法将 'this' 指针从 'std::stack<_Ty>' 转换为 'std::stack<_Ty> &'

c++ - 两个日期之间有多少天 C++

c++ - std::cout 中的递归打印

使用 MinGW-w64 和 Boost.Build 的 C++ 构建环境

c++ - boost 转换产生 Inf 返回值

C++ boost 正则表达式 : How to find all possible string constants from C/C++ code?

c++ - boost spirit 词素及其属性

C++ Primer 第 9 章无法编译 : `useConvs.cc:50:19: error: call of overloaded ‘stod(std::string&)’ is ambiguous`

c++ - boost::spirit 。将(名称)(描述)文本解析为 map

c++ - 指示 Qi 转换属性失败的正确方法是什么?