Proper usage of "thru" in parse
[1/5] from: mediquip8:yah:oo at: 20-Jul-2006 7:39
I want to use parse to print item1,item2,item3 and item4 in the string "text" below.
How does one get "thru" to search either <normal> or <highlight> before copying item
1, item2, item3 and item4?
text:"<normal>item1<end><highlight>item2<end><highlight>item3<end><normal>item4<end>"
I have tried the "or" inside the rule but could not get it to work.
rule: [thru {"<normal>"|"<hightlight>"} copy label to {<end>} (print label) ]
parse text [any[rule]]
Any help will be appreciated. Thank you.
-Mark Chang
Malaysia
---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates starting
at 1¢/min.
[2/5] from: edoconnor:gm:ail at: 20-Jul-2006 12:00
The parse keywords to and thru are a bit tricky -- they zoom through
the string until they find a match or fail.
What you need to do is step through the input one character at a time,
testing for a match at each step. In this case, avoid using to and
thru -- use skip instead.
parse/all text [any [
<normal> copy label to <end> (print label) |
<highlight> copy label to <end> (print label) |
skip
]
to end
]
The english statement of the above would be:
Go through all of the text, including spaces.
Repeat the following rule exhaustively through the text. At each character:
1. if match the tag! value <normal> copy the text until the <end>
tag and print it
2. if match the tag! value <highlight> copy the text until the <end>
tag and print it
3. no match, so skip to the next character in text string and return to step 1
Go to the end of the text string, which returns "true" to the general
parse statement.
Be careful about the mistake of surrounding sub-rules for parse in
curly braces. They are sub-blocks and need to be enclosed in [].
I usually find it best to keep the parse rules as basic as possible,
and then refine/condense them if I'm confident they work as intended.
My slightly bloated example above could be streamlined to:
end-tag: [to <end>]
rule: [any [
<normal> copy i end-tag (print i) |
<highlight> copy i end-tag (print i) |
skip
]
to end
]
>> parse/all text rule
item1
item2
item3
item4
== true
Regards,
Ed
On 7/20/06, Mark Chang <mediquip8-yahoo.com> wrote:
[3/5] 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
[4/5] from: compkarori::gmail::com at: 21-Jul-2006 8:15
I think this is an easier way to do things for those new to parse - convert
it first to datatype! and then use block parsing.
text:"<normal>item1<end><highlight>item2<end><highlight>item3<end><normal>item4<end>"
==
{<normal>item1<end><highlight>item2<end><highlight>item3<end><normal>item4<end>}
>> data: load/markup text
== [<normal> "item1" <end> <highlight> "item2" <end> <highlight> "item3"
<end> <normal> "item4" <end>]
>> parse data [ some [ copy item tag! | copy item string! (print item) ]]
item1
item2
item3
item4
== true
--
Graham Chiu
http://www.compkarori.com/emr/
[5/5] from: Tom::Conlin::gmail::com at: 21-Jul-2006 1:23
for a non parse solution
>> foreach item load/markup text[if not tag? item[print item]]
item1
item2
item3
item4
Mark Chang wrote:
> I want to use parse to print item1,item2,item3 and item4 in the string text
below. How does one get "thru" to search either <normal> or <highlight> before copying
item 1, item2, item3 and item4?