我将如何解析这个字符串
"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/