【问题标题】:Split a string using C++ boost::split without splitting inside quoted text使用 C++ boost::split 拆分字符串而不拆分引用的文本
【发布时间】:2012-11-04 13:49:42
【问题描述】:

我正在使用

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

当然,我可以在标记的开头和结尾搜索引号字符,并使用“”合并以引号开头的标记和以引号结尾的标记之间的标记,以重新创建带引号的字符串,但是我想知道是否有更有效/更优雅的方式来做到这一点。有什么想法吗?

【问题讨论】:

  • 欢迎来到 Stack Overflow!我对你的问题感到困惑。这个问题的第一段是否与问题的其余部分有任何关系?
  • boost::tokenizer 可能有您需要的功能。
  • 拆分可能过于简单,因为您确实需要某种形式的解析
  • @Yakk 是的。要拆分的字符串看起来像 string("command_name first_argument \"Second argument which is aquoted string.\"" )
  • @Robᵩ 不,这很奇怪。第一段是我昨天在另一个帖子上发表的评论。如果我编辑这个当前问题,我根本看不到第一段,所以我无法删除它......奇怪的行为。

标签: c++ boost split


【解决方案1】:

使用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;
}

输出:

命令名 first_argument 第二个参数是一个带引号的字符串。

https://ideone.com/gwCpug 上查看演示。

【讨论】:

  • 有没有办法保留引号?我需要输出如下:command_namefirst_argument"Second argument which is a quoted string."
【解决方案2】:

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

【讨论】:

  • 您可以通过存储bool* in_q 而不是mutable in_q 来修复const 破坏此问题。反斜杠转义状态应该很容易添加。
  • 我关心的不是使用 mutable 关键字,而是实际违反不变性
  • 如果将指针存储在函子中,则不会违反不变性。变异函子的最大问题是您依赖于算法来仅使用函子的一个实例,而不是复制 - 如果发生这种情况,您会遇到问题。我可以看到的指针版本的唯一问题是算法可能会乱序运行。
  • @Yakk 谢谢你详细描述了这种方法的问题
最近更新 更多