Parsing 如何在Rebol中解析它?
我如何解析这个字符串Parsing 如何在Rebol中解析它?,parsing,rebol,Parsing,Rebol,我如何解析这个字符串 "a:foo[and it's cousin bar[are here]]" 进入这个 "a:" "foo[" "and" "it's" "cousin" "bar[" "are" "here" "]" "]" 本质上,我希望完成三件事,提取作业“a:”,提取节“foo[”(包括嵌套节)和结束节“]”。我可以将它们均匀地隔开,只做一个简单的解析,但我不想这样做 希望它有意义。任何帮助都将不胜感激 谢谢 关于这个示例的更多上下文可能会有所帮助,因为您可以在rebol中尝试
"a:foo[and it's cousin bar[are here]]"
进入这个
"a:" "foo[" "and" "it's" "cousin" "bar[" "are" "here" "]" "]"
本质上,我希望完成三件事,提取作业“a:”,提取节“foo[”(包括嵌套节)和结束节“]”。我可以将它们均匀地隔开,只做一个简单的解析,但我不想这样做
希望它有意义。任何帮助都将不胜感激
谢谢 关于这个示例的更多上下文可能会有所帮助,因为您可以在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中很少以这种方式使用字符串。块通常更灵活、更易于解析。定义语言的元素,然后在匹配它们时收集它们:
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
来隔离仅用于此目的的单词。哇,非常感谢Chris!这正是我要找的!感谢您的时间和专业知识。