Boost拆分不遍历括号或大括号内 [英] Boost split not traversing inside of parenthesis or braces

查看:80
本文介绍了Boost拆分不遍历括号或大括号内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试拆分以下文本:

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

关于括号和花括号,如何实现?

Regarding parentheses and braces, how to implement it?

推荐答案

您要解析语法.

自从您标记了您使用Boost Spirit:

Since you tagged with boost let me show you using Boost Spirit:

在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

这篇关于Boost拆分不遍历括号或大括号内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆