Split using boost :: is_any_of confuses delimeter "," and ","

I currently have a line that has the following structure

xxx,xxx,xxxxxxx,,xxxxxx,xxxx 

Now I use the following code

  std::vector< std::string > vct; boost::split( vct, str, boost::is_any_of(",,") ); 

Now boost breaks the line as soon as it finds "," and not ",", which I don't want. Is there a way that I could explicitly indicate that it should split only if it finds "," and not ","

+4
source share
4 answers

is_any_of(",,") will match everything in the list. In this case, either, or ,

What you are looking for is on the line

 boost::algorithm::split_regex( vct, str, regex( ",," ) ) ; 
+6
source

For future reference ..

boost :: split accepts the 4th parameter of eCompress , which allows the processing of neighboring dividers as one separator:

eCompress

If the eCompress argument is set to token_compress_on, adjacent delimiters are combined together. Otherwise, divide the token every two separators.

All you have to do is specify a parameter. You can also skip the second , , for example:

 boost::split( vct, str, boost::is_any_of(","), boost::algorithm::token_compress_on) 

Here is the documentation.

+3
source

Is_any_of splits into any of the characters in the string. He will not do what you want. You need to look in the acceleration guide for another predicate.

Edit: Out of curiosity, I was looking for an API myself, I could not find a ready-made predicate for what you want. In the worst case, you have to write it yourself.

0
source
 #include <functional> #include <boost/algorithm/string/compare.hpp> ... std::vector< std::string > vct; //boost::split( vct, str, [](const auto& arg) { return arg == str::string(",,"); } ); boost::split( vct, str, std::bind2nd(boost::is_equal, std::string(",,")) ); 
0
source

All Articles