Conditional Parsing
[1/3] from: gchillemi::aliceposta::it at: 6-May-2005 1:35
Is there a way to conditionally parse a String in only one run ? The target
of this question if to find a way to PARSE in one command a text page which
could have data formatted in different way or new data in certain situation.
Here is an example:
This is the usual page:
Name: John
Surname: Doe
Book: Private America
1stPubblication: 1950
Brief description: oo xx oo xx oo xx
Name: John2
Surname: Doe2
Book: Private America2
1stPubblication: 1950
Brief description: oo xx oo xx oo xx 2
Name: John3
Surname: Doe3
Book: Private America3
1stPubblication: 1950
Brief description: oo xx oo xx oo xx 3
This is the same page with differences:
Name: John
Surname: Doe
Book: Private America
1stPubblication: 1950
Brief description: oo xx oo xx oo xx
(Here is the difference. This Is a comment, consider it as non existent)
Name: Jane
Surname: Doe
Height: 1.75m
Eyes: Brown
Hair: Blonde
Hobbies: Play Tennis, Swim, Music
Book: Private Women of America
1stPubblication: 1950
Brief description: oo xx oo xx oo xx Woman
Name: John2
Surname: Doe2
Book: Private America2
1stPubblication: 1950
Brief description: oo xx oo xx oo xx 2
Thank you in advance.
Giuseppe Chillemi
[2/3] from: apwing::zonnet::nl at: 6-May-2005 16:32
Hi Giuseppe,
does the following script help you a bit?
Kind regards,
Arie
=================================== script follows
=============================================
rebol []
example1: {
Name: John
Surname: Doe
Book: Private America
1stPubblication: 1950
Brief description: oo xx oo xx oo xx
}
example2: {
Name: Jane
Surname: Doe
Height: 1.75m
Eyes: Brown
Hair: Blonde
Hobbies: Play Tennis, Swim, Music
Book: Private Women of America
1stPubblication: 1950
Brief description: oo xx oo xx oo xx Woman
}
rule: [
thru "Name: "
copy name
to "^/"
(print ["Name: " name])
thru "^/"
thru "Surname: "
copy surname
to "^/"
(print ["Surname: " surname])
thru "^/"
0 1 [
thru "Height: "
copy height
to "^/"
(print ["Height: " height])
thru "^/"]
0 1 [
thru "Eyes: "
copy Eyes
to "^/"
(print ["Eyes: " Eyes])
thru "^/"]
0 1 [
thru "Hair: "
copy Hair
to "^/"
(print ["Hair: " hair])
thru "^/"]
0 1 [
thru "Hobbies: "
copy Hobbies
to "^/"
(print ["Hobbies: " Hobbies])
thru "^/"]
thru "Book: "
copy book
to "^/"
(print ["Book: " book])
thru "^/"
thru "1stPubblication: "
copy first_publ
to "^/"
(print ["1stPubblication: " first_publ])
thru "^/"
thru "Brief description: "
copy brief_descr
to "^/"
(print ["Brief description: " brief_descr])
thru "^/"
]
print ""
print "Parsing example1"
print "================"
print parse/all example1 rule
print ""
print "Parsing example2"
print "================"
print parse/all example2 rule
halt
=========================================== end of script
============================================
Giuseppe Chillemi wrote:
[3/3] from: ammon::johnson::gmail::com at: 6-May-2005 10:18
What are you trying to do with the output, create a block of Key,
Value pairs? Create an Object!? That's going to change the way you
want to parse it alot. Do you need to be able to capture unkown
fields
in the text or do you want to ignore those fields? You said
you need to be able capture different formats. Well if you want to
capture different formats then you'll need to define every format that
the file could come in within a single parse rule, and yes this is
possible but I don't have enough information to really be of much
help. I can give you a few pointers though...
I would the SOME [ rule1 | rule2 ] notation to be able to capture
multiple "fields" in an unspecified order. Also with SOME you can
capture multiple formats very easily as it simply checks the separate
rules until none of them match. If you want to capture "fields" whose
name you are not aware of and ":" is the seperating value between the
field name and the value then a rule something like: SOME [ rule1 |
.... | copy field to ":" copy value to newline ] Would allow you to
capture every field as long as the character that denotes the end of
the value is a newline character.
HTH!!
~~Ammon ;~>
On 5/6/05, Arie van Wingerden <[apwing--zonnet--nl]> wrote:
> Hi Giuseppe,
>
> does the following script help you a bit?
>
> Kind regards,
> Arie
>
> =========================
=========== script follows
> =========================
================
> rebol []
> example1: {
<<quoted lines omitted: 76>>
> halt
> =========================
=================== end of script
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted