我正在尝试解析一些字符串输入,但我很难看到解决方案。然而,这必须是一个众所周知的模式 - 它只是我不经常遇到的模式。
背景:我有一个简短的字符串关键字列表(“HEAD”,“GET”,“POST”,“PUT”),每个关键字后面都有其他字符串数据。按任何顺序可以有多个序列(“关键字,等等等等等等等等等等等)”。 XML没有终止字符或结束关键字 - 关键字子句的新出现或输入的结束。样品:
str: {HEAD stuff here GET more stuff here POST other stuff here GET even more stuff here PUT still more stuff here POST random stuff}
我想要实现的输出:
results: [
"HEAD" ["stuff here"]
"GET" ["more stuff here" "even more stuff here"]
"POST" ["other stuff here" "random stuff"]
"PUT" ["still more stuff here"]
]
我对此的不良尝试是:
results: ["head" [] "get" [] "post" [] "put" []]
rule1: ["HEAD" (r: "head") | "GET" (r: "get") | "POST" (r: "post") | "PUT" (r: "put")]
rule2: [to "HEAD" | to "GET" | to "POST" | to "PUT" | to end]
parse/all str [
some [
start: rule1 rule2 ending:
(offs: offset? start ending
append select results r trim copy/part start offs
) :ending
| skip]
]
我知道规则2是笨蛋 - 使用“to”操作符并不是思考这种模式的正确方法;当我希望它找到任何关键字时,它会跳到该规则块中第一个可用关键字的下一个出现位置。
任何提示都将不胜感激。
答案 0 :(得分:2)
这个怎么样......
;; parse rules
keyword: [{HEAD} | {GET} | {POST} | {PUT}]
content: [not keyword skip]
;; prep results block... ["HEAD" [] "GET" [] "POST" [] "PUT" []]
results: []
forskip keyword 2 [append results reduce [keyword/1 make block! 0]]
parse/case str [
any [
copy k keyword copy c some content (
append results/:k trim c
)
]
]
使用str
然后results
将拥有您想要的内容....
["HEAD" ["stuff here"] "GET" ["more stuff here" "even more stuff here"] "POST" ["other stuff here" "random stuff"] "PUT" ["still more stuff here"]]
答案 1 :(得分:2)
也许不那么优雅,但即使与Rebol2合作
results: ["HEAD" [] "GET" [] "POST" [] "PUT" []]
keyword: [{HEAD} | {GET} | {POST} | {PUT}]
parse/case str [
any [
[copy k keyword c1: ] | [skip c2:]
[[keyword | end] (
append results/:k trim copy/part c1 c2
) :c2 |
]
]
]
答案 2 :(得分:1)
这是另一种变体。
str: {HEAD stuff here GET more stuff here POST other stuff here GET even more stuff here PUT still more stuff here POST random stuff}
results: ["HEAD" [] "GET" [] "POST" [] "PUT" []]
possible-verbs: [ "HEAD" | "GET" | "POST" | "PUT" | end ]
parse/all str [
some [
to possible-verbs
verb-start: (verb: first split verb-start " ")
possible-verbs
copy text to possible-verbs
(if not none? verb [ append results/:verb trim text ])
]
]
probe results
同样,在优雅和方法相似方面并不完美。