如何在url中解析/着色关键字/值对?

时间:2010-06-25 20:33:24

标签: rebol

我试图在rebol中着色这样的网址

content: "http://domain.com/test.php?keyword=hdhdf&hdhd=sdcfsv&sbcfsv=sdncfd&sncfsdv=dncsv&cnsv=dshdkd&scsv=12334&DXV=D&SWJDJJDFDJQKKKKKKKKKKKK&DFG=V&DJJF=DJVNVV&DJFFFFFFFFFF=33333"

rule-keyword-0: [to "?" thru "?" mark: (insert mark {<font color="red">}) 19 skip to "=" mark: (insert mark "</font>") thru "="]
rule-keyword-1: [to "&" thru "&" mark: (insert mark {<font color="red">}) 19 skip to "=" mark: (insert mark "</font>") thru "="] 

rule-value-0: [to "</font>=" thru "</font>=" mark: (insert mark {<font color="blue">}) 20 skip to "&" mark: (insert mark "</font>") thru "&"]
rule-value-1: [to "</font>=" thru "</font>=" mark: (insert mark {<font color="blue">}) 20 skip to end mark: (insert mark "</font>")] 

rule-keyword: [any [rule-keyword-0 | rule-keyword-1] to end]
rule-value: [any [rule-value-0 | rule-value-1] to end]

parse content rule-keyword
parse content rule-value

但是输出不正确(例如,参见双字体颜色=“蓝色”):

http://domain.com/test.php?<font color="red">keyword</font>=<font color="blue">hdhdf</font>&<font color="red">hdhd</font>=<font color="blue">sdcfsv</font>&<font color="red">sbcfsv</font>=<font color="blue">sdncfd</font>&<font color="red">sncfsdv</font>=<font color="blue">dncsv</font>&<font color="red">cnsv</font>=<font color="blue">dshdkd</font>&<font color="red">scsv</font>=<font color="blue">12334</font>&<font color="red">DXV</font>=<font color="blue">D&<font color="red">SWJDJJDFDJQKKKKKKKKKKKK</font>&DFG</font>=<font color="blue">V&<font color="red">DJJF</font>=DJVNVV</font>&<font color="red">DJFFFFFFFFFF</font>=<font color="blue"><font color="blue">33333</font>

正确的规则

2 个答案:

答案 0 :(得分:1)

可能有更优雅的规则,但这似乎适用于您的数据,假设我已经猜到了您想要的内容。

   content: "http://domain.com/test.php?keyword=hdhdf&hdhd=sdcfsv&sbcfsv=sdncfd&sncfsdv=dncsv&cnsv=dshdkd&scsv=12334&DXV=D&SWJDJJDFDJQKKKKKKKKKKKK&DFG=V&DJJF=DJVNVV&DJFFFFFFFFFF=33333"


    result: parse content [
        thru "?"
        some [
            ; we should be at the beginning of the pairs
            mark1: 
            copy stuff to "=" mark2: (
                ; to ensure that there is a pair here
                if stuff [
                    insert mark2 </font>
                    insert mark1 <font color="red">
                ]
            )
            ; find the = sign
            thru </font> thru #"="
            mark1:
            [ copy stuff to #"&" | copy stuff to end ]
            mark2: 
            (   if stuff [
                    insert mark2 </font> 
                    insert mark1 <font color="blue">
                ]
            )   
            thru </font>
            [ thru "&" | end ]  
        ]
    ]

    ?? result
    ?? content

答案 1 :(得分:0)

您没有指定正确的输出会是什么样的,因此提交错误的代码并要求我们猜测您尝试做的事情有点多!像往常一样,我会建议将你的例子减少到尽可能小的再现你的问题。 (在您不得不提出问题之前,这通常会引导您找到解决方案!)

http://catb.org/esr/faqs/smart-questions.html#code

但是在袖口之外,我怀疑你正在经历括号中的任何代码在规则匹配期间执行的事实,无论规则是否结束匹配。看看这个简单的例子:

>> rule-1: ["a" (print "a matched in rule-1") "b"]
== ["a" (print "a matched in rule-1") "b"]

>> rule-2: ["a" (print "a matched in rule-2") "c"]
== ["a" (print "a matched in rule-2") "c"]

>> parse "ac" [any [rule-1 | rule-2]]
a matched in rule-1
a matched in rule-2
== true

虽然第一条规则失败了,但你会得到两个打印输出!来自rule-1的打印输出是因为括号中的代码在确定失败之前执行。

你的“任何”运行两个可能匹配或不匹配的规则,在确定完全匹配之前都进行插入,看起来像你的问题。