Treetop/PEG 中的非贪婪匹配?
我将如何在 Treetop 中做这样的事情?
/.+?;/
似乎唯一的方法是:
[^;]+ ';'
这有点丑……还有其他方法吗? .+?
似乎不起作用..
How would I do something like this in Treetop?
/.+?;/
It seems like the only way is to do:
[^;]+ ';'
Which is kind of ugly.. any other way? .+?
doesn't seem to work..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,PEG 是贪婪和盲目的,这意味着它们会吃掉尽可能多的输入,并且不考虑之后会发生什么:
S <- P1* P2
(贪婪、盲目)这可能相当大通过使用有序选择(并且不使用前瞻)可以轻松修复:
S <- P1 S / P2
(贪婪,非盲)S <- P2 / P1 S
(懒惰,非盲目)PEGs are greedy and blind by default, that means they eat as much input as they can and they do not consider what comes afterwards:
S <- P1* P2
(greedy, blind)That can be considerably easy fixed though by making use of the ordered choice (and without using lookaheads):
S <- P1 S / P2
(greedy, non-blind)S <- P2 / P1 S
(lazy, non-blind)嗯,我知道 PEG 是贪婪的,而且没有办法绕过它。 不过,可以使用前瞻来模仿这种行为,例如
!(';' .)
Well, I learnt PEGs are greedy, and there's no way around it. Lookaheads can be used to mimic this behavior though, like
!(';' .)
我不知道 Treetop,但是
/[^;]+;/
可以工作吗?通过快速搜索,我看到建议 Treetop 不执行贪婪或惰性(非贪婪)量词,并且
+
实际上是一个所有格量词(由++ 其他正则表达式风格)。
如果是这种情况,我不确定除了否定类之外,您还有其他基于正则表达式的选项。
I don't know Treetop, but would
/[^;]+;/
work?From a quick search, I saw suggestion that Treetop doesn't do greedy nor lazy (non-greedy) quantifiers, and that the
+
is actually a possessive quantifier (represented by++
in other regex flavours).If this is the case, I'm not sure you've got any other regex-based options than the negated class.