如何解析/着色 url 中的关键字/值对?
我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能有更优雅的规则,但这似乎适用于您的数据,假设我已经猜到您想要什么。
There are probably more elegant rules but this seems to work for your data, assuming that I have guessed what you want.
您没有指定正确的输出是什么样子,因此提交错误的代码并要求我们猜测您想要做什么有点过分了!像往常一样,我会建议将您的示例减少到能够重现您的问题的最小程度。 (这通常会在您提出问题之前找到解决方案!)
http://catb.org/esr/faqs/smart-questions.html#code
但是,我怀疑您正在经历这样一个事实:括号中的任何代码都会在规则匹配期间执行,无论规则如何最终匹配或不匹配。看这个简单的例子:
虽然第一条规则失败了,但你得到了两个打印输出!
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:
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.