I'm trying to parse currency values with Rebol 2/3 from a string, the currency values are in the format:
10,50 € OR 10,50€
This code I came up with after going through all PARSE documentation I could find works in Red, but not in Rebol 2 or 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)
]
]
After playing around with this for quite a while I came to the conclusion that TO/THRU doesn't work with numbers for some reason (it does seem to work fine with characters), but I can't figure out how to parse this without TO/THRU since the string has arbitrary contents that need to be skipped over.
(from tryrebol)
44,55€
66,44 €
*** 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 [
...
*** 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
Using TO and THRU isn't ideal for pattern matching (at least in Rebol at this time).
If you're only searching for currency, you can create a rule that either matches currency or steps forward:
digit: charset [#"0" - #"9"]
digits: [some digit]
euro: [digits opt ["," digits] any " " "€"]
parse "hello 22 44,55€ 66,44 € 33 world" [
; can use ANY as both rules will advance if matched
any [
copy yank euro (probe yank)
| skip ; no euro here, move forward one
]
]
Note that as EURO is a complete match for the currency sequences you're looking for, you can just use COPY to assign the sequence to a word. This should work in both Rebol and Red (though you'll need to use PARSE/ALL in Rebol 2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With