2

I have a string that I want to split, so I'm using boost::split

However boost::is_any_of takes a string and uses each of the characters as delimiters.

My delimiters should be "->" and ":"

":" works since it's a single character delimiter, but "->" doesn't (it takes each character ("-" and ">" separately as a delimiter)

std::vector<std::string> strs;
boost::split(strs, line, boost::is_any_of(["->:"]));

How can I define multiple delimiters with some of them being more than one character?

Example:

"0:c->2"   should give [0,"c",2]

I'm open to other solutions not using boost if they are easier for that particular problem

  • 1
    related/dupe: https://stackoverflow.com/questions/15789449/c-boost-split-a-string-on-more-than-one-character – NathanOliver Dec 06 '18 at 14:56
  • @VictorGubin in the tokenizer link I see them using single character separators, could you provide an example of how to use "->" and ":" as separators? –  Dec 06 '18 at 15:12
  • @VictorGubin This example uses either `-` `;` or `|` as separators. I don't see how they can be used to split when you find `->` but not split when you find `-` or `>` alone –  Dec 06 '18 at 16:32

1 Answers1

2

You can use Boost.Spirit to parse the string:

#include <string>
#include <vector>
#include <iostream>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

int main()
{
    std::string str = "0:c->2";
    std::vector< std::string > vec;

    auto it = str.begin(), end = str.end();
    bool res = qi::parse(it, end,
        qi::as_string[ *(qi::char_ - ':' - "->") ] % (qi::lit(':') | qi::lit("->")),
        vec);

    std::cout << "Parsed:";
    for (auto const& s : vec)
        std::cout << " \"" << s << "\"";

    std::cout << std::endl;
    return 0;
}

Here, the parser produces a list of strings that match the *(qi::char_ - ':' - "->") parser (which reads as "any number of any characters, except ':' or "->""), separated by strings that match the (qi::lit(':') | qi::lit("->")) parser (which reads as "either ':' character or "->" string"). The first parser must exclude the separators, otherwise they will be included in the parsed string. The qi::as_string part simply converts the parsed characters to a std::string attribute, which is then appended to the vec sequence.

Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27