我正在使用 boost 字符串库,并且刚刚遇到了 split 方法的简单性。
string delimiters = ",";
string str = "string, with, comma, delimited, tokens, \"and delimiters, inside a quote\"";
// If we didn't care about delimiter characters within a quoted section we could us
vector<string> tokens;
boost::split(tokens, str, boost::is_any_of(delimiters));
// gives the wrong result: tokens = {"string", " with", " comma", " delimited", " tokens", "\"and delimiters", " inside a quote\""}
这会很好而且简洁......但是它似乎不适用于引号,而是我必须做类似以下的事情
string delimiters = ",";
string str = "string, with, comma, delimited, tokens, \"and delimiters, inside a quote\"";
vector<string> tokens;
escaped_list_separator<char> separator("\\",delimiters, "\"");
typedef tokenizer<escaped_list_separator<char> > Tokeniser;
Tokeniser t(str, separator);
for (Tokeniser::iterator it = t.begin(); it != t.end(); ++it)
tokens.push_back(*it);
// gives the correct result: tokens = {"string", " with", " comma", " delimited", " tokens", "\"and delimiters, inside a quote\""}
我的问题是,当您引用分隔符时,可以使用拆分或其他标准算法吗?多亏了purpledog,但我已经有了一种不被弃用的方法来实现预期的结果,我只是认为它很麻烦,除非我可以用更简单更优雅的解决方案替换它,否则我一般不会在不先将其包装的情况下使用它另一种方法。
编辑:更新代码以显示结果并澄清问题。