【问题标题】:Boost split not traversing inside of parenthesis or bracesBoost split 不在括号或大括号内遍历
【发布时间】:2015-09-26 00:34:53
【问题描述】:

我尝试拆分以下文本:

std::string text="1,2,3,max(4,5,6,7),array[8,9],10,page{11,12},13";

我有以下代码:

std::vector<std::string> found_list;
boost::split(found_list,text,boost::is_any_of(","))

但我想要的输出是:

1
2
3
max(4,5,6,7)
array[8,9]
10
page{11,12}
13

关于圆括号和大括号,如何实现?

【问题讨论】:

    标签: c++ c++11 boost split tokenize


    【解决方案1】:

    你想解析一个语法

    既然你用 标记了,让我向你展示如何使用 Boost Spirit:

    Live On Coliru

    #include <boost/spirit/include/qi.hpp>
    
    namespace qi = boost::spirit::qi;
    
    int main() {
        std::string const text="1,2,3,max(4,5,6,7),array[8,9],10,page{11,12},13";
    
        std::vector<std::string> split;
    
        if (qi::parse(text.begin(), text.end(),
                    qi::raw [
                        qi::int_ | +qi::alnum >> (
                            '(' >> *~qi::char_(')') >> ')'
                          | '[' >> *~qi::char_(']') >> ']'
                          | '{' >> *~qi::char_('}') >> '}'
                        )
                    ] % ',',
                    split))
        {
            for (auto& item : split)
                std::cout << item << "\n";
        }
    }
    

    打印

    1
    2
    3
    max(4,5,6,7)
    array[8,9]
    10
    page{11,12}
    13
    

    【讨论】:

    • 任何在线参考qi::parse的定义?
    • 除了声明函数之外的所有内容。我想知道所有参数、它们的类型和可能的函数重载。
    • 我专门链接到那个页面。如果您是 Spirit 新手,请从教程开始。
    • 在任何教程之前都必须声明。新手可能会对 boost 教程感到更加困惑。 Boost教程很差。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    相关资源
    最近更新 更多