Boost split not traversing inside of parenthesis or braces

拟墨画扇 提交于 2019-12-11 03:05:39

问题


I try to split the following text:

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

I have the following code:

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

But my desired output is:

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

Regarding parentheses and braces, how to implement it?


回答1:


You want to parse a grammar.

Since you tagged with boost let me show you using 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";
    }
}

Prints

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


来源:https://stackoverflow.com/questions/32792425/boost-split-not-traversing-inside-of-parenthesis-or-braces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!