跳转到内容

Rebol 编程/语言特性/解析/简单拆分

来自维基教科书,开放的书籍,开放的世界

字符串解析涉及简单拆分

parse "this is a string" none
; == ["this" "is" "a" "string"]

通过提供 NONE 作为 PARSE 规则,我们要求 PARSE 基于空格将字符串拆分为字符串块

whitespace: charset [#"^A" - #" " "^(7F)^(A0)"]

和公共分隔符

common-delimiter: charset ",;"

为了便于 CSV 拆分,引号将被特殊处理(参见 CSV 示例)。

空字符串

[编辑 | 编辑源代码]
parse "" none
; == []

输入字符串中没有分隔符

[编辑 | 编辑源代码]
parse "redbluegreen" none
; == ["redbluegreen"]
parse "red blue green" none
; == ["red" "blue" "green"]
parse "red,blue,green" none
; == ["red" "blue" "green"]

制表符

[编辑 | 编辑源代码]
parse "red^-blue^-green" none
; == ["red" "blue" "green"]
parse "red;blue;green" none
; == ["red" "blue" "green"]

换行符

[编辑 | 编辑源代码]
string: {
red
blue  
green
}
parse string none
; == ["red" "blue" "green"]

忽略前导和尾随空格

[编辑 | 编辑源代码]
parse " 1 " none
; == ["1"]

空格序列等同于一个空格

[编辑 | 编辑源代码]
parse "1  2" none
; == ["1" "2"]

前导公共分隔符会分隔一个空子字符串

[编辑 | 编辑源代码]
parse ",1" none
; == ["" "1"]

忽略一个尾随公共分隔符

[编辑 | 编辑源代码]
parse "1," none
; == ["1"]

公共分隔符序列在它们之间分隔空子字符串

[编辑 | 编辑源代码]
parse "1,,2" none
; == ["1" "" "2"]
parse {"red","blue","green"} none
; == ["red" "blue" "green"]
华夏公益教科书