跳轉到內容

Rebol 程式設計/語言特性/解析/使用特定分隔符進行分割

來自華夏公益教科書,開放書籍,為開放世界

parse string none 表示式在以下情況下會失效:

  • 你想要指定哪些字元應該被視為空格
  • 你想要指定哪些字元應該被視為分隔符
  • 你不想對引號進行特殊處理
  • 你需要其他非簡單的分割方式

這些示例展示瞭如何影響 PARSE 函式處理空格的方式,或者如何使用特定分隔符來代替預設分隔符。

要消除空格的特殊處理,請使用 /ALL PARSE 細化

[編輯 | 編輯原始碼]
parse/all "only common delimiters; split the text, now" none
; == ["only common delimiters" " split the text" " now"]

如果你有不同的分隔符,你可以向 PARSE 提供包含你的分隔符的字串規則。

"#" 字元作為分隔符

[編輯 | 編輯原始碼]
parse "red#blue#green" "#"
; == ["red" "blue" "green"]

空格字元作為分隔符

[編輯 | 編輯原始碼]
parse/all "red blue^-green" " "
; == ["red" "blue^-green"]

"#" 和 "*" 字元作為分隔符

[編輯 | 編輯原始碼]
parse "red#blue*green" "#*"
; == ["red" "blue" "green"]

請注意,分隔符字串中字元的順序並不重要。

華夏公益教科書