微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用C boost :: split拆分字符串而不拆分引用文本

我在用
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_name
first_argument
Second argument which is a quoted string.

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

原文地址:https://www.jb51.cc/c/115667.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐