-2

I want to split each line by the appearance of <=> or => So I have 2 delimiters and each of them has more than one character

  string example="A + B => C + D";
  vector <string> leftRight;
  boost::algorithm::split_regex( leftRight, example, boost::is_any_of( " <=> +| => " ));

my expected result is something like this:

leftright[0]= A+B
leftright[1]= C+D
MoonBun
  • 4,322
  • 3
  • 37
  • 69
user1876128
  • 91
  • 14

1 Answers1

1

So, let's look at boost::algorithm::split_regex. You're doing well until the last parameter. That function expects a boost::regex as the last parameter, and boost::is_any_of does not provide one of those.

A reasonable regular expression for your use case would be something like this:

boost::regex r("(<=>)|(=>)");

If we put all of this together:

#include <vector>
#include <string>
#include <iostream>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>

int main() {
    std::string example="A + B => C + D";
    std::vector <std::string> leftRight;
    boost::regex r("(<=>)|(=>)");
    boost::algorithm::split_regex(leftRight, example, r);

    for (int i=0; i<leftRight.size(); ++i)
        std::cout << "\"" << leftRight[i] << "\"\n";
}

We get an output of:

"A + B "
" C + D"
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173