在 Red 语言中,如何使用 split 拆分字符串,同时保留分隔符

In Red language, how to split a string using split, but also keep the delimiters as nessecary

我想用split拆分一个字符串,同时该字符串包含用作分隔符的字符串,不应将其视为分隔符。我尝试过这种方式,如下代码所示:

>> split {1 + 3 `to-string #"^(60)"`c} "`"
== ["1 + 3 " {to-string #"} {"} "c"] ;;I think it should be ["1 + 3 " {to-string #"^(60)"}"c"] 
这里使用

to-string #"^(60)"是为了避免出现“``”,这会被称为分隔符,但它失败了。 ^(60) 似乎被评估为“```”,然后被 split 用作分隔符。 那么,Red语言中split的机制是什么?以及如何在拆分字符串的同时保留那些不应被视为分隔符的分隔符。

^(60) 是所谓的 代码点形式 ,它作为 ` 字符加载。

>> "^(60)"
== "`"

如果你想避免这种情况,你应该手动转义它:

>> {1 + 3 `to-string #"^^(60)"` c}
== {1 + 3 `to-string #"^^(60)"` c}

或使用原始字符串:

>> %{1 + 3 `to-string #"^(60)"` c}%
== {1 + 3 `to-string #"^^(60)"` c}

split事后添加它是微不足道的:

>> split %{1 + 3 `to-string #"^(60)"` c}% #"`"
== ["1 + 3 " {to-string #"^^(60)"} " c"]

如果你想保留 ` 字符在那里,那么 split 不会削减它。你需要像 Parse:

这样的东西
>> string: {1 + 3 `to-string #"`"` c}
== {1 + 3 `to-string #"`"` c}
>> parse string [collect [keep to " `" " `" keep to "` " "` " keep copy match to end]]
== ["1 + 3" {to-string #"`"} "c"]
>> parse string [collect some [keep copy _ to copy match [" `" | "` " | end] match]]
== ["1 + 3" {to-string #"`"} "c"]