Perhaps someone could tell me what is happening here?
My intention is to split an input string on braces: ie: either ‘(‘ or ‘)‘.
For an input string of “(well)hello(there)world” I expect 4 tokens to be returned: well; hello; there; world.
As you can see from my exemplar app below I am getting 5 tokens back (The 1st is an empty string).
Is there any way to get this to return me only the non-empty strings?
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <vector>
int main()
{
std::string in = "(well)hello(there)world";
std::vector<std::string> tokens;
boost::split(tokens, in, boost::is_any_of("()"));
for (auto s : tokens)
std::cout << "\"" << s << "\"" << std::endl;
return 0;
}
Output:
$ a.out
"" <-- where is this token coming from?
"well"
"hello"
"there"
"world"
I have tried using boost::algorithm::token_compress_on but I get the same result.
Yes, the first result returned is the empty set {} immediately preceding the first open parenthesis. The behavior is as expected.
If you don’t want to use that result, simply test for an empty returned variable and discard it.
To test that this is the expected behavior, put a parenthesis at the end of the line and you will have another empty result at the end. 🙂