我在用
@H_403_2@boost::split(strs,r_strCommandLine,boost::is_any_of("\t "));
将字符串吐入标记以解析简单脚本.到现在为止还挺好.但是,对于以下字符串
@H_403_2@command_name first_argument "Second argument which is a quoted string."我想要我的代币
@H_403_2@strs[0] = command_name strs[1] = first_argument strs[2] = "Second argument which is a quoted string."当然,我可以在标记的开头和结尾搜索引号字符,并使用“”分隔符合并标记以引号开头的事件和以引号结束的标记之间的标记,以重新创建引用的字符串,但我想知道如果有一种更有效/更优雅的方式来做到这一点.有任何想法吗?
解决方法
使用
boost::tokenizer
的示例:
@H_403_2@#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;
}
输出:
@H_403_2@ command_name first_argument Second argument which is a quoted string.请参阅https://ideone.com/gwCpug的演示.