[REBOL] Re: Rebol parsing 101
From: ingo::2b1::de at: 2-Oct-2003 10:56
Hi Mike,
Mike Weber wrote:
> im brand new to Rebol and not getting the hang of string parsing
>
> for example: assume i have string
> fcontents
> == {09/29/03 ATM/POS ACTIVITY $28.68 (pending) 09/29/03 ATM/POS ACTIVITY $11.41 09/29/03
ATM/POS ACTIVITY $
> 21.71 ...
>
> i would like to convert this string into a set of blocks where each block has 4 elements
of the types [date string money string] (the 4th element is optional
> [09/29/03 "ATM/POS ACTIVITY" $28.68 (pending)]
> [09/29/03 "ATM/POS ACTIVITY" $11.41 ]
> [09/29/03 "ATM/POS ACTIVITY" $21.71 ]
the following works with your example string, but it may choke on embedded
newlines (at the >>any " "<<).
;----------- start ------------
s: {09/29/03 ATM/POS ACTIVITY $28.68 (pending) 09/29/03 ATM/POS ACTIVITY
$11.41 09/29/03 ATM/POS ACTIVITY $2.11}
; create a charset, matching non numbers
non-number: complement charset "0123456789"
; create an empty block for the resutl
b: copy []
; parse/all so that parse doesn't eat spaces ...
parse/all s [
; we want the folloowing more than once
some [
; get the date (for some reason, rebol does not understand this
; dateformat, so we have to get the individual date parts seperatly
copy dm 2 skip "/"
copy dd 2 skip "/"
copy dy 2 skip skip
; the string ends at the start of the money
copy s1 to "$"
; money either ends with a space, or it may be the last element in the
; string
copy m [thru " " | to end]
; there may some or no space now (maybe newlines? if your string gets
; dynamically created ...
any " "
; now append what we found so far to the result block, rebuild the date,
; so that rebol understands it
(append/only b compose [(to-date rejoin [dd "/" dm "/" dy] ) (s1) (load
m) ])
; there MAY be a string now, this will start with any character, NO
; number, otherwise you're in trouble here ... anyway, if there's the
; optional string, append it to the last block in your result block
; if there's no string to be found here, we may even have reached the end,
; and be done
opt [copy s2 any non-number (if not none? s2 [append last b s2]) | end]
]
]
;------------ end ------------
I hope that gets you going,
Ingo