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 拆分字符串而不拆分带引号的文本
基础教程推荐
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
