使用 C++ boost::split 拆分字符串,而不在引号文本内拆分

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

本文关键字:拆分 文本 boost C++ split 字符串 使用      更新时间:2023-10-16

我正在使用

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." 

当然,我可以在令牌的开头和结尾搜索引号字符,并使用" "分隔符在以引号开头的令牌和以引号结尾的令牌之间合并以重新创建带引号的字符串,但我想知道是否有更有效/优雅的方法这样做。有什么想法吗?

使用 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_namefirst_argument第二个参数是带引号的字符串。

请参阅 https://ideone.com/gwCpug 的演示。

我不确定这个解决方案是可移植的(我们违反了 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