如何使用PARSE解析字符串中的货币值

Ser*_*nse 5 parsing rebol

我正在尝试使用字符串中的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)

Rebol 3:

*** 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)

Rebol 2:

*** 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)

rgc*_*ris 3

使用 TO 和 THRU 对于模式匹配来说并不理想(至少目前在 Rebol 中)。

\n\n

如果您仅搜索货币,则可以创建匹配货币或前进的规则:

\n\n
digit: 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]\n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,由于欧元与您正在查找的货币序列完全匹配,因此您可以仅使用 COPY 将序列分配给单词。这应该适用于 Rebol 和 Red(尽管您需要在 Rebol 2 中使用 PARSE/ALL)。

\n