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

发布于 2024-09-06 17:57:22 字数 2119 浏览 1 评论 0原文

我尝试在 rebol 中对这样的 url 进行着色,

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

但输出不正确(例如,请参见最后的 double font color="blue"):

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>

正确的规则是什么

I tried to colorize in rebol an url like this

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

But output is not right (see for example double font color="blue" at the end):

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>

What the correct rule

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-09-13 17:57:22

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

   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

There are probably more elegant rules but this seems to work for your data, assuming that I have guessed what you want.

   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
薄荷→糖丶微凉 2024-09-13 17:57:22

您没有指定正确的输出是什么样子,因此提交错误的代码并要求我们猜测您想要做什么有点过分了!像往常一样,我会建议将您的示例减少到能够重现您的问题的最小程度。 (这通常会在您提出问题之前找到解决方案!)

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 的打印输出之所以发生,是因为在确定失败之前执行了括号中的代码。

您的“任何”运行两个可能匹配也可能不匹配的规则,两者在找出完整匹配之前都进行插入,看起来像是您的问题。

You didn't specify what the correct output would look like, and so submitting incorrect code and asking us to guess what you were trying to do is a bit much! I will, as usual, suggest reducing your example to the smallest possible that reproduces your problem. (That will often lead you to the solution before you have to ask the question!)

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

But off the cuff, I suspect you are experiencing the fact that any code in parentheses is executed during the rule match, whether the rule winds up matching or not. Look at this simple example:

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

Though the first rule failed, you get both printouts! The printout from rule-1 happened because the code in parentheses executed before the failure had been determined.

Your "any" running two rules that may or may not match, both doing insertions before figuring out the full match, looks like your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文