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
但是假设我正在寻找一个包含苹果符号的 block ,然后是与字符串规则
匹配的任意数量的字符串:
; 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)?
最佳答案
史蒂夫在 REBOL3 forum建议这样做:
only the second string is checked.
Should be:
['apple some [and string! into ["a" some "b" ]]]
关于string - 如何在同一规则中混合字符串解析和 block 解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6392533/