想象一个简单的例子,我们想把字符串"0-5"
变成"012345"
.
这有效:
>> parse "0-5" [
set a char!
"-"
set b char!
(
while [a <= b] [
prin a
a: a + 1
]
)
]
结果是:
012345
true
但是,如果我想要更一般的东西,那可能会"10-12"
变成"101112"
,或者更远呢?这不起作用:
>> parse "0-5" [
set a [some char!]
"-"
set b [some char!]
(
a-int: to integer! a
b-int: to integer! b
while [a-int <= b-int] [
prin to string! a-int
a-int: a-int + 1
]
)
]
它不起作用的原因是因为它没有set a [some char!]
捕获字符串,而是错误:
>> parse "10" [set a [some char!] (print a)]
** Script error: PARSE - invalid rule or usage of rule: char
** Where: parse
** Near: parse "10" [set a [some char] (print a)]
作为一个额外的问题,为什么会失败并且使用 skip 只返回第一个数字?
>> parse "10" [set a [some skip] (print a)]
1
== true
...whensome skip
和some char!
match 在字符串上等效,因为字符串中唯一要跳过的是字符...?