Split a string using C++ boost::split without splitting inside quoted text(使用 C++ boost::split 拆分字符串而不拆分带引号的文本)
问题描述
我正在使用
boost::split(strs, r_strCommandLine, boost::is_any_of(" "));
将字符串吐出到用于解析简单脚本的标记中.到现在为止还挺好.但是,对于下面的字符串
command_name first_argument "第二个参数是一个带引号的字符串."
我希望我的代币成为
strs[0] = command_namestrs[1] = first_argumentstrs[2] = "第二个参数是带引号的字符串."
当然,我可以在标记的开头和结尾搜索引号字符,并使用"来分隔以引号开头的标记和以引号结尾的标记的出现之间的标记以重新创建带引号的字符串,但是我想知道是否有更有效/更优雅的方式来做到这一点.有什么想法吗?
解决方案使用 的示例
boost::tokenizer
:#include
#include 使用 std::cout;使用 std::string;#include 使用 boost::tokenizer;使用 boost::escaped_list_separator;typedef tokenizer >so_tokenizer;int main(){string s("command_name first_argument"""第二个参数是带引号的字符串."");so_tokenizer tok(s, escaped_list_separator ('\', ' ', '"'));for(so_tokenizer::iterator beg=tok.begin(); beg!=tok.end();++beg){cout<<*乞求<<" ";}返回0;} 输出:
<前>命令名称第一个参数第二个参数是带引号的字符串.
在 https://ideone.com/gwCpug 上查看演示.
I am using
boost::split(strs, r_strCommandLine, boost::is_any_of(" "));
to spit a string into tokens for parsing a simple script. So far, so good. However, for the following string
command_name first_argument "Second argument which is a quoted string."
i would like my tokens to be
strs[0] = command_name
strs[1] = first_argument
strs[2] = "Second argument which is a quoted string."
Of course, I could search for quote characters at beginning and ending of tokens and merging using " " delimiters the tokens between the the occurrence of a token beginning with a quote and a token ending with a quote to recreate the quoted string but I am wondering if there is a more efficient/elegant way of doing this. Any ideas?
Example using 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 << "
";
}
return 0;
}
Output:
command_name first_argument Second argument which is a quoted string.
See demo at https://ideone.com/gwCpug .
这篇关于使用 C++ boost::split 拆分字符串而不拆分带引号的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 C++ boost::split 拆分字符串而不拆分带引号的文本


- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01