我正在使用
boost::split(strs, r_strCommandLine, boost::is_any_of("\t "));
将字符串吐入标记以解析简单脚本。到现在为止还挺好。但是,对于以下字符串
command_name first_argument "Second argument which is a quoted string."
我希望我的代币是
strs[0] = command_name
strs[1] = first_argument
strs[2] = "Second argument which is a quoted string."
当然,我可以在令牌的开头和结尾搜索引号字符,并使用“”分隔符合并以引号开头的令牌和以引号结尾的令牌之间的标记,以重新创建引用的字符串但是我想知道是否有更有效/更优雅的方式来做到这一点。有什么想法吗?
答案 0 :(得分:14)
使用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;
}
输出:
command_name first_argument Second argument which is a quoted string.
请参阅https://ideone.com/gwCpug上的演示。
答案 1 :(得分:0)
我不确定这个解决方案是否可移植(我们违反bool operator() (char ch) const
的const条件),但它确实有效。
这个解决方案在理论上很有意思,我不会在实际项目中使用它。
#include <boost/algorithm/string/split.hpp>
#include <string>
#include <vector>
#include <iostream>
class split_q {
public:
split_q() : in_q(false) {}
bool operator() (char ch) const
{
if (ch == '\"') in_q = !in_q;
return !in_q && ch == ' ';
}
private:
mutable bool in_q;
};
int main(int argc, char* argv[])
{
std::string in = "command_name first_argument \"Second argument which is a quoted string.\" additional_argument";
std::vector<std::string> res;
boost::algorithm::split(res, in, split_q());
for (size_t i = 0; i < res.size(); ++i)
std::cout << res[i] << std::endl;
return 0;
}
结果:
command_name
first_argument
"Second argument which is a quoted string."
additional_argument