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?
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With