我如何在 Rebol 中解析这个?

发布于 2024-12-05 00:48:58 字数 312 浏览 2 评论 0 原文

我将如何将此字符串解析

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

为 this

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

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

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

谢谢!

How would I go about parsing this string

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

into this

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

In essence I'm looking to accomplish three things, extract an assignment "a:", extract sections "foo[" (including nested sections) and the closing section "]". I could evenly space them and just do a simple parse but I don't want to do that.

Hope it makes sense. Any help will be greatly appreciated!

Thanks!

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

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

发布评论

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

评论(2

蛮可爱 2024-12-12 00:48:58

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

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 来隔离仅用于此目的的单词。

Define the elements of your language, then collect them as you match them:

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

Note: I use 'use to isolate words used solely for this purpose.

2024-12-12 00:48:58

围绕该示例的更多上下文可能会有所帮助,因为您通常可以在 rebol 中尝试许多选项。

一种简单的方法是“修复”字符串,使其更像正常的 rebol 数据。

source-string: "a:foo[and it's cousin bar[are here]]"
replace/all source-string "[" " [ "
replace/all source-string "]" " ] "
replace/all source-string ":" ": "
output: load source-string

在rebol中这样使用字符串的情况很少见。块通常更灵活且易于解析。

Some more context around the example may help as there are often many options you can try in rebol.

One simple approach would be to "fix" your string to make it more like normal rebol data.

source-string: "a:foo[and it's cousin bar[are here]]"
replace/all source-string "[" " [ "
replace/all source-string "]" " ] "
replace/all source-string ":" ": "
output: load source-string

It is rare to use strings in this way in rebol. Blocks are generally more flexible and simple to parse.

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