3

在这里查看 html 示例:http ://www.red-lang.org/2013/11/041-introducing-parse.html

我想解析以下内容:

"val1-12*more text-something"

在哪里:

  • "-"标记应该在同一块中的值,并且
  • "*"应该开始一个新的块。

所以,我想要这个:

[ ["val1" "12"] ["more text" "something"] ]

此刻我得到了这个:

red>> data: "val1-12*more text-something"
== "val1-12*more text-something"

red>> c: charset reduce ['not #"-" #"*"]
== make bitset! [not #{000000000024}]

red>> parse data [collect [any [keep any c [#"-" | #"*" | end ]]]]
== ["val1" "12" "more text" "something"]

(我实际上尝试了其他一些排列,但并没有让我走得更远。)

那么,缺少什么?

4

1 回答 1

2

您可以通过嵌套 COLLECT 使其工作。例如

keep-pair: [
    keep some c 
    #"-"
    keep some c 
] 

parse data [
    collect [
        some [
            collect [keep-pair]
            #"*"
            collect [keep-pair]
        ]
    ]
]

使用您的示例输入,这将输出您想要的结果:

[["val1" "12"] ["more text" "something"]]


但是我有一种有趣的感觉,您可能希望解析规则比提供的示例输入更灵活?

于 2014-02-27T23:11:30.267 回答