2

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?

ar2015
  • 5,558
  • 8
  • 53
  • 110

1 Answers1

3

You want to parse a grammar.

Since you tagged with 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
sehe
  • 374,641
  • 47
  • 450
  • 633
  • any online reference to definition of `qi::parse`? – ar2015 Sep 26 '15 at 08:49
  • Yeah. It's in the boost documentation http://www.boost.org/doc/libs/1_59_0/libs/spirit/doc/html/spirit/qi.html – sehe Sep 26 '15 at 08:53
  • It is about everything but declaring the function. I want to know all the arguments, their types and possible function overloads. – ar2015 Sep 26 '15 at 10:45
  • I specifically linked to that page. If you're new to Spirit, start with the tutorial. – sehe Sep 26 '15 at 19:25
  • A declaration must be placed before any tutorial. A new person might get more confused by boost tutorial. Boost tutorial is very poor. – ar2015 Sep 26 '15 at 23:30
  • Yeah. The "boost tutorial" is very very poor. Because it doesn't exist. I happen to know that it is not possible to learn the basics of the Spirit from the reference. Anyhow, since you might be an exceptionally fast learner (and the above example is enough tutorial), but you didn't see the "reference" section" next to the "tutorial" section [in that same page](http://www.boost.org/doc/libs/1_59_0/libs/spirit/doc/html/spirit/qi.html), here goes: – sehe Sep 27 '15 at 10:39
  • http://www.boost.org/doc/libs/1_59_0/libs/spirit/doc/html/spirit/qi/reference/parse_api/iterator_api.html – sehe Sep 27 '15 at 10:39
  • http://www.boost.org/doc/libs/1_59_0/libs/spirit/doc/html/spirit/qi/reference/numeric/int.html – sehe Sep 27 '15 at 10:40
  • http://www.boost.org/doc/libs/1_59_0/libs/spirit/doc/html/spirit/qi/reference/char/char.html – sehe Sep 27 '15 at 10:40
  • http://www.boost.org/doc/libs/1_59_0/libs/spirit/doc/html/spirit/qi/reference/directive/raw.html – sehe Sep 27 '15 at 10:40
  • http://www.boost.org/doc/libs/1_59_0/libs/spirit/doc/html/spirit/qi/reference/operator/list.html – sehe Sep 27 '15 at 10:40