如何在同一规则中混合字符串解析和块解析?

发布于 2024-11-16 04:05:09 字数 1160 浏览 2 评论 0原文

很酷的是,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

递刀给你 2024-11-23 04:05:09

Steeve 在 REBOL3 论坛 上建议:

only the second string is checked.
Should be:
    ['apple some [and string! into ["a" some "b" ]]]

Steeve over on the REBOL3 forum suggests this:

only the second string is checked.
Should be:
    ['apple some [and string! into ["a" some "b" ]]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文