[REBOL] Re: Proper usage of "thru" in parse
From: greggirwin::mindspring::com at: 20-Jul-2006 10:08
Hi Mark,
MC> I want to use parse to print item1,item2,item3 and item4 in the
MC> string "text" below. How does one get "thru" to search either
MC> <normal> or <highlight> before copying item 1, item2, item3 and
MC> item4?
MC> text:"<normal>item1<end><highlight>item2<end><highlight>item3<end><normal>item4<end>"
Here is one way:
rule: [
to "<" skip ["normal" | "highlight"] ">"
copy label to "<end>" 5 skip (print label)
]
parse/all text [any [rule]]
A catch with THRU, in what you think would be simple cases, is that
parse doesn't match the first thing in the stream that matches any of
the tokens you OR together; it tries each one and will match the first
occurrence of any of them in the order they appear in the ORed block;
so you can end up skipping over a lot of data unintentionally. It is
often simpler to take a non-greedy, more explicit, approach, even if
it seems like a little more work.
-- Gregg