且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用REBOL解析HTML标记内?

更新时间:2023-12-05 21:28:58

LOAD/MARKUP处理mess时,您会得到以下提示(并且我已经格式化并加上了类型注释):

When mess is processed with LOAD/MARKUP you get this (and I've formatted + commented with 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's href in quotes above...)

    ; string!
    "^/"

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

    ; string!
    "^/"
]

您的输出模式匹配形式为[<td> string! </td>]的系列,但不匹配形式为[<td> tag! string! tag! </td>]的事物.避开标题中提出的问题,您可以通过几种方法解决此特殊难题.一种可能是保持对您是否在TD标签中的计数,并在计数不为零时打印任何字符串:

Your output pattern matches series of the form [<td> string! </td>] but not things of the form [<td> tag! string! tag! </td>]. Sidestepping the question posed in your title, you could solve this particular dilemma several ways. One might be to maintain a count of whether you are inside a TD tag and print any strings when the count is non-zero:

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!
    ]
]

这将产生您要求的输出:

This produces the output you asked for:

Bob Sockaway
bsockaway@example.com
9999

但是您还本质上想知道您是否可以从同一规则集的块解析(不跳入开放代码)过渡到字符串解析.我研究了它的混合解析",好像它是Rebol 3中解决的功能.不过,我仍然无法在实践中使用它.所以我问了一个自己的问题.

But you also wanted to know, essentially, whether you can transition into string parsing from block parsing in the same set of rules (without jumping into open code). I looked into it "mixed parsing" looks like it may be a feature addressed in Rebol 3. Still, I couldn't get it to work in practice. So I asked a question of my own.

如何混合