How to parse HTML tags using REBOL?

I have a webpage that I loaded with download / markup. I need to parse a bunch of things, but some data is in the tags. Any ideas on how I can make it out? Here is an example of what I have (and tried):

REBOL []

mess: {
<td>Bob Sockaway</td>
<td><a href=mailto:bsockaway@example.com>bsockaway@example.com</a></td>
<td>9999</td>
}

rules: [
    some [
        ; The expression below /will/ work, but is useless because of specificity.
        ; <td> <a href=mailto:bsockaway@example.com> s: string! </a> (print s/1) </td> | 

        ; The expression below will not work, because <a> doesn't match <a mailto=...>
        ; <td> <a> s: string! </a> (print s/1) </td> |

        <td> s: string! (print s/1) </td> |

        tag! | string! ; Catch any leftovers.
    ]
]

parsing / markup rules

This gives:

Bob Sockaway
9999

I would like to see something more:

Bob Sockaway
bsockaway@example.com
9999

Any thoughts? Thank!

Attention! For what it's worth, I came up with a nice simple set of rules that will get the desired results:

rules: [
    some [
        <td> any [tag!] s: string! (print s/1) any [tag!] </td> |
        tag! | string! ; Catch any leftovers.
    ]
]
+3
source share
2 answers

When messprocessed using LOAD/MARKUP, you get this (and I formatted + commented on the types):

[
    ; string!
    "^/" 

    ; tag! string! tag!
    <td> "Bob Sockaway" </td>

    ; string!
    "^/"

    ; tag! tag!
    ;     string!
    ; tag! tag!
    <td> <a href=mailto:bsockaway@example.com>
        "bsockaway@example.com"
    </a> </td>

    ; (Note: you didn't put the anchor href in quotes above...)

    ; string!
    "^/"

    ; tag! string! tag!
    <td> "9999" </td> 

    ; string!
    "^/"
]

[<td> string! </td>], [<td> tag! string! tag! </td>]. , , . , TD , :

rules: [
    (td-count: 0)
    some [
        ; if we see an open TD tag, increment a counter
        <td> (++ td-count)
        |
        ; if we see a close TD tag, decrement a counter
        </td> (-- td-count)
        |
        ; capture parse position in s if we find a string
        ; and if counter is > 0 then print the first element at
        ; the parse position (e.g. the string we just found) 
        s: string! (if td-count > 0 [print s/1])
        |
        ; if we find any non-TD tags, match them so the
        ; parser will continue along but don't run any code
        tag!
    ]
]

, :

Bob Sockaway
bsockaway@example.com
9999

, , ( ). " ", , , Rebol 3. , . .

?

+2

, . , , , .

id !:

<query id="5">

parse tag!, :

  | set t tag! (
    p: make block! t 
    if p/1 = 'query [_qid: to-integer p/3]
  )

, , . , , _qid

to-integer select p 'id=

,

switch p/1 [
  field [_fid: to-integer p/id= _field_type: p/field_type=]
  query [_qid: to-integer p/id=]
]
+1

All Articles