- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我介于对 boost::spirit 的深深钦佩和不理解它的永恒挫折之间;)
我的字符串过于贪婪,因此不匹配。下面是一个不解析的最小示例,因为 txt 规则吃完了。
有关我想做的事情的更多信息:目标是解析一些伪 SQL,我跳过空格。在类似
的声明中select foo.id, bar.id from foo, baz
我需要将 from
视为特殊关键字。规则类似于
"select" >> txt % ',' >> "from" >> txt % ','
但它显然不起作用,因为它将 foo 的 bar.id
视为一个项目。
#include <boost/spirit/include/qi.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
int main(int, char**) {
auto txt = +(qi::char_("a-zA-Z_"));
auto rule = qi::lit("Hello") >> txt % ',' >> "end";
std::string str = "HelloFoo,Moo,Bazend";
std::string::iterator begin = str.begin();
if (qi::parse(begin, str.end(), rule))
std::cout << "Match !" << std::endl;
else
std::cout << "No match :'(" << std::endl;
}
最佳答案
这是我的版本,更改标记为:
#include <boost/spirit/include/qi.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
int main(int, char**) {
auto txt = qi::lexeme[+(qi::char_("a-zA-Z_"))]; // CHANGE: avoid eating spaces
auto rule = qi::lit("Hello") >> txt % ',' >> "end";
std::string str = "Hello Foo, Moo, Baz end"; // CHANGE: re-introduce spaces
std::string::iterator begin = str.begin();
if (qi::phrase_parse(begin, str.end(), rule, qi::ascii::space)) { // CHANGE: used phrase_parser with a skipper
std::cout << "Match !" << std::endl << "Remainder (should be empty): '"; // CHANGE: show if we parsed the whole string and not just a prefix
std::copy(begin, str.end(), std::ostream_iterator<char>(std::cout));
std::cout << "'" << std::endl;
}
else {
std::cout << "No match :'(" << std::endl;
}
}
这可以用 GCC 4.4.3 和 Boost 1.4something 编译和运行;输出:
Match !
Remainder (should be empty): ''
通过使用 lexeme
,您可以避免有条件地吃掉空格,这样 txt
只匹配一个单词边界。这会产生预期的结果:因为 "Baz"
后面没有逗号,而且 txt
不占用空格,所以我们不会意外地使用 "end"
.
无论如何,我不能 100% 确定这就是您要查找的内容——特别是 str
是否缺少空格作为说明性示例,或者您是否以某种方式被迫使用此 (无空格)格式?
旁注:如果您想确保已解析整个字符串,请添加一个检查以查看是否 begin == str.end()
。如前所述,即使仅解析了 str
的非空前缀,您的代码也会报告匹配项。
更新 添加后缀打印。
关于c++ - Boost精神太贪心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5355715/
>>> import re >>> p = re.compile('.*&l=(.*)(&|$)') >>> p.search('foo&l=something here&bleh').group(1
最近有一道面试题如下:我们得到了一个单词列表,我们想要格式化它们以最大化回车符的数量,同时将每行的字母数量保持在一个范围内。 例如,我们希望每行的字母范围为 5 - 10(含),一种解决方案是: he
我正在使用二维数组来处理游戏中的对象。数组的维度就像笛卡尔网格上的坐标。当玩家选择一个点时,我想从数组中收集 N 个最近的网格单元,即使该点不是有效的数组索引。 例子:从 [0,0] 到 [10,10
我在 Hibernate 之上使用 Olingo 1.2。 我有一个返回 250 行的请求,每行以一对多关系链接到另一个表。 我执行 $expand 以获取子表中的所有数据,但是当我检查在数据库中执行
我正在 ANTLR4 中构建语法,但收到此警告 TL4.g4:224:12: greedy block ()* contains wildcard;非贪婪语法 ()*?可能是首选 这是它引用的代码行
In the default greedy mode, all data offered to targets are accepted, even if the other target doesn
假设我有 n 个盒子,每个盒子里面都有一些值 b[i] .我可以保证对一组框进行排序,使得 b[1] j; { min_{k=i}^j (c[k] + max(T(i, k-1)
本文已收录到 AndroidFamily ,技术和职场问题,请关注公众号 [彭旭锐] 提问。 大家好,我是小彭。 上周末是 LeetCode 第 339 场周赛,你参加
什么是 PHP 中的“贪心 token 解析”?我在 Codeigniter 指南中找到了这个: “除非需要解析变量,否则始终使用单引号字符串,并且在确实需要解析变量的情况下,使用大括号防止贪婪的标记
本文已收录到 AndroidFamily ,技术和职场问题,请关注公众号 [彭旭锐] 提问。 大家好,我是小彭。 上周末是 LeetCode 第 337 场周赛,你参加
我是一名优秀的程序员,十分优秀!