Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string using C++ boost::split without splitting inside quoted text

Tags:

c++

split

boost

I am using

boost::split(strs, r_strCommandLine, boost::is_any_of("\t "));

to spit a string into tokens for parsing a simple script. So far, so good. However, for the following string

command_name first_argument "Second argument which is a quoted string." 

i would like my tokens to be

strs[0] = command_name
strs[1] = first_argument
strs[2] = "Second argument which is a quoted string." 

Of course, I could search for quote characters at beginning and ending of tokens and merging using " " delimiters the tokens between the the occurrence of a token beginning with a quote and a token ending with a quote to recreate the quoted string but I am wondering if there is a more efficient/elegant way of doing this. Any ideas?

like image 693
Christian O'Reilly Avatar asked Nov 15 '12 21:11

Christian O'Reilly


1 Answers

Example using boost::tokenizer:

#include <string>
#include <iostream>
using std::cout;
using std::string;

#include <boost/tokenizer.hpp>
using boost::tokenizer;
using boost::escaped_list_separator;

typedef tokenizer<escaped_list_separator<char> > so_tokenizer;

int main()
{
    string s("command_name first_argument "
             "\"Second argument which is a quoted string.\"");

    so_tokenizer tok(s, escaped_list_separator<char>('\\', ' ', '\"'));
    for(so_tokenizer::iterator beg=tok.begin(); beg!=tok.end(); ++beg)
    {
        cout << *beg << "\n";
    }

    return 0;
}

Output:

command_name
first_argument
Second argument which is a quoted string.

See demo at https://ideone.com/gwCpug .

like image 162
hmjd Avatar answered Nov 03 '22 19:11

hmjd