如何在同一规则中混合字符串解析和块解析?
很酷的是,Rebol 的 PARSE 方言足够通用,它可以对符号结构以及字符串进行模式匹配和提取。像这样:
; match a single "a" character, followed by any number of "b" chars
>> string-rule: ["a" some "b"]
>> parse "abb" string-rule
== true
>> parse "aab" string-rule
== false
; look for a single apple symbol, followed by any number of bananas
>> block-rule: ['apple some 'banana]
>> parse [apple banana banana] block-rule
== true
>> parse [apple apple banana] block-rule
== false
但是假设我正在寻找一个包含苹果符号的块,然后是与字符串规则匹配的任意数量的字符串:
; test 1
>> parse [apple "ab" "abbbbb"] mixed-rule
== true
; test 2
>> parse [apple "aaaa" "abb"] mixed-rule
== false
; test 3
>> parse [banana "abb" "abbb"] mixed-rule
== false
我将如何制定这样的混合规则?查看文档,它建议人们可以使用 INTO:
http://www.rebol.net/wiki /Parse_Project#INTO
看似自然的答案似乎不起作用:
>> mixed-rule: ['apple some [string! into ["a" some "b"]]]
虽然它通过了测试 1 并为测试 3 正确返回 false,但在测试 2 中错误地返回 true。这是我的错误还是 Rebol 中的错误(我正在使用r3 A111)?
It's cool that Rebol's PARSE dialect is generalized enough that it can do pattern matching and extraction on symbolic structures as well as on strings. Like this:
; match a single "a" character, followed by any number of "b" chars
>> string-rule: ["a" some "b"]
>> parse "abb" string-rule
== true
>> parse "aab" string-rule
== false
; look for a single apple symbol, followed by any number of bananas
>> block-rule: ['apple some 'banana]
>> parse [apple banana banana] block-rule
== true
>> parse [apple apple banana] block-rule
== false
But let's say I'm looking for a block containing an apple symbol, and then any number of character strings matching the string-rule
:
; test 1
>> parse [apple "ab" "abbbbb"] mixed-rule
== true
; test 2
>> parse [apple "aaaa" "abb"] mixed-rule
== false
; test 3
>> parse [banana "abb" "abbb"] mixed-rule
== false
How would I formulate such a mixed-rule
? Looking at the documentation it suggests that one can use INTO:
http://www.rebol.net/wiki/Parse_Project#INTO
The seemingly natural answer doesn't seem to work:
>> mixed-rule: ['apple some [string! into ["a" some "b"]]]
While it passes test 1 and correctly returns false for test 3, it incorrectly returns true in test 2. Is this my mistake or a bug in Rebol (I'm using r3 A111)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Steeve 在 REBOL3 论坛 上建议:
Steeve over on the REBOL3 forum suggests this: