[REBOL] Re: Parsing out strings
From: Gary:Jones:usap:gov at: 30-Apr-2004 11:13
From: ML
....
> Suppose the line I am parsing says:
> "the user jsmith logged in at 4.30pm"
>
> Can you give me a clue what I would use to
> parse in each of these circumstances where
> the desired text is:
>
> 1. "the user"
> 2. "the user jsmith"
> 3. "jsmith"
> 4. "jsmith logged in at 4.30pm"
> 5. "logged in at 4.30pm"
Hi, Stuart,
You will get a hundred ways to do this. In order to illustrate the more general principles,
I offer the following ways (watch for line wrap). The first is a bit "brute force":
data: "the user jsmith logged in at 4.30pm"
parse-bits: parse data none
probe rejoin [parse-bits/1 " " parse-bits/2]
probe rejoin [parse-bits/1 " " parse-bits/2 " " parse-bits/3]
probe parse-bits/3
probe rejoin [parse-bits/3 " " parse-bits/4 " " parse-bits/5 " " parse-bits/6 " " parse-bits/7]
probe rejoin [parse-bits/4 " " parse-bits/5 " " parse-bits/6 " " parse-bits/7]
This first method relies on the fact of the number of words of each record in the log
remain the same. The second method is more rule based, and it copies sections based
on the rules:
data: "the user jsmith logged in at 4.30pm"
parse/all data [
copy p1
thru "the user"
copy p2
to "logged"
copy p3
to end
]
trim p2
print [p1 p2 p3]
and finally a variation that demonstrates a slightly different way with more parse commands:
data: "the user jsmith logged in at 4.30pm"
parse/all data [
copy p1
thru "the user"
skip
copy p2
to " "
skip
copy p3
to end
]
print [p1 p2 p3]
Hope that helps get you started on the REBOL ways to parse.
--Scott Jones