加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何在评论或引用时忽略分隔符

发布时间:2020-12-16 07:30:46 所属栏目:百科 来源:网络整理
导读:我编写了一个解析器来查找字符串连接表达式.我有一系列用括号括起来的字符串,主要来自函数调用. 例如,(“一”“两”“三”) – (“一个”|“两个”|“三个”)是一个简单的案例,我可以处理它. 更难的情况是(null,“one”“two”“three”,null) – (null,“on
我编写了一个解析器来查找字符串连接表达式.我有一系列用括号括起来的字符串,主要来自函数调用.

例如,(“一”“两”“三”) – > (“一个”|“两个”|“三个”)是一个简单的案例,我可以处理它.

更难的情况是(null,“one”“two”“three”,null) – > (null,“one”|“two”|“three”,null),但我能用boost :: tokenizer解析它.

(null,“one”“two”“three,four”,1 / *第三个参数可以是:1,2,3 * /),在这样一个困难的例子中我建议用boost :: spirit解析但是我需要帮助为它写一些规则.

后来:

看起来像boost :: tokenizer中的escaped_list_separator就是我需要的.
但我有一个问题:

using namespace std;
   using namespace boost;
   string s = "Field 1,"putting quotes around fields,allows commas",Field 3";
   tokenizer<escaped_list_separator<char> > tok(s,escaped_list_separator<char>("",","""));
   for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end();++beg){
       cout <<"~~~"<< *beg << "n";
   }

删除“对我来说.可以像这样在输出中保留引号

Field 1
"putting quotes around fields,allows commas"
Field 3

解决方法

基本上,您可以使用operator-with charset matches:

rule = '"' >> (char_ - '"') >> '"';

另请查看operator~反转字符集.

如果你有兴趣在引号内转义引号,也许同时评论样式,我建议你在这里查看我的答案:

> How to make my split work only on one real line and be capable to skip quoted parts of string?

在CSV文件中显示(部分)引用的单元格,包括字符串中的转义引号.

其他感兴趣的项目:

> Cross-platform way to get line number of an INI file where given option was found

忽略评论中的内容并转义评论分隔符
> Parse quoted strings with boost::spirit

显示各种样式的带引号的字符串解析,包括嵌入的转义引号.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读