parsing - 我如何在 Rebol 中解析这个?

标签 parsing rebol

我将如何解析这个字符串

"a:foo[and it's cousin bar[are here]]"

进入此

"a:" "foo[" "and" "it's" "cousin" "bar[" "are" "here" "]" "]"

本质上,我希望完成三件事,提取赋值“a:”,提取部分“foo[”(包括嵌套部分)和结束部分“]”。我可以均匀地间隔它们并只进行简单的解析,但我不想这样做。

希望这是有道理的。任何帮助将不胜感激!

谢谢!

最佳答案

定义您的语言的元素,然后在匹配它们时收集它们:

parse-my-language: use [word assignment section section-end space][

    word: use [letters][
        letters: charset [#"a" - #"z" ".'"]
        [some letters]
    ]
    assignment: [word ":"]
    section: [word "["]
    section-end: "]"

    space: charset " "

    func [statement /local out element][
        out: copy []
        if parse/all statement [
            any [
                copy element [
                    assignment | section | section-end | word
                ] (append out element)
                | some space
            ]
        ][out]
    ]
]

probe parse-my-language "a:foo[and it's cousin bar[are here]]"

注意:我使用'use来隔离仅用于此目的的单词。

关于parsing - 我如何在 Rebol 中解析这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7438408/

相关文章:

rebol - 如何在 Rebol 中的特定字符处拆分字符串

parsing - 如何在 Clojure 中懒惰地解析大 XHTML 文件?

linux - 如何计算linux中的百分比

java - 如何在 Java 中一次解析 DTD 文件而不进行任何验证

c# - 使用 LINQ 将文本文件解析为 CSV

html - 解析此 "simple"html 页面的巨大问题

rebol - 如何将 block 中的 set-words 转换为单词

python - 使用 StanfordParser 从已解析的句子中获取类型依赖项

string - 如何在同一规则中混合字符串解析和 block 解析?

xml - 如何使用 REBOL 发送带有自定义 header 的 HTTP post