我正在尝试使用字符串中的Rebol 2/3解析货币值,货币值的格式如下:
10,50€或10,50€
我通过所有PARSE文档找到了这段代码,我发现它可以用Red实现,但不能用于Rebol 2或3.
digit: charset [#"0" - #"9"]
digits: [some digit]
euro: [digits "," digits [any " "] "€"]
parse "hello 22 44,55€ 66,44 € 33 world" [
some [
to euro
copy yank thru euro
(print yank)
]
]
Run Code Online (Sandbox Code Playgroud)
在玩了很长一段时间之后,我得出的结论是TO/THRU由于某种原因不适用于数字(它似乎与字符一起使用),但是我无法弄清楚如何解析它TO/THRU因为字符串具有需要跳过的任意内容.
(来自tryrebol)
44,55€
66,44 €
Run Code Online (Sandbox Code Playgroud)
*** ERROR
** Script error: PARSE - invalid rule or usage of rule: [some digit]
** Where: parse try do either either either -apply-
** Near: parse "hello 22 44,55€ 66,44 € 33 world" [
some [
...
Run Code Online (Sandbox Code Playgroud)
*** ERROR
code: 305
type: script
id: invalid-arg
arg1: [digits "," digits [any " "] "€"]
arg2: none
arg3: none
near: [parse "hello 22 44,55€ 66,44 € 33 world" [
some [
to euro
copy yank thru euro
(print yank)
]
]]
where: none
Run Code Online (Sandbox Code Playgroud)
使用 TO 和 THRU 对于模式匹配来说并不理想(至少目前在 Rebol 中)。
\n\n如果您仅搜索货币,则可以创建匹配货币或前进的规则:
\n\ndigit: charset [#"0" - #"9"]\ndigits: [some digit]\n\neuro: [digits opt ["," digits] any " " "\xe2\x82\xac"]\n\nparse "hello 22 44,55\xe2\x82\xac 66,44 \xe2\x82\xac 33 world" [\n ; can use ANY as both rules will advance if matched\n any [\n copy yank euro (probe yank)\n | skip ; no euro here, move forward one\n ]\n]\nRun Code Online (Sandbox Code Playgroud)\n\n请注意,由于欧元与您正在查找的货币序列完全匹配,因此您可以仅使用 COPY 将序列分配给单词。这应该适用于 Rebol 和 Red(尽管您需要在 Rebol 2 中使用 PARSE/ALL)。
\n