Jump to content

Rebol Programming/Language Features/Parse/Simple splitting

From Wikibooks, open books for an open world

String parsing involves simple splitting:

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

By providing NONE as the PARSE rule, we are asking PARSE to break a string into a block of string(s) based on whitespace:

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

and common delimiters:

common-delimiter: charset ",;"

To facilitate CSV splitting, quotation marks are handled specially (see the CSV example).

Examples

[edit | edit source]

Empty string

[edit | edit source]
parse "" none
; == []

No delimiters in the input string

[edit | edit source]
parse "redbluegreen" none
; == ["redbluegreen"]

Space

[edit | edit source]
parse "red blue green" none
; == ["red" "blue" "green"]

Comma

[edit | edit source]
parse "red,blue,green" none
; == ["red" "blue" "green"]
parse "red^-blue^-green" none
; == ["red" "blue" "green"]

Semicolon

[edit | edit source]
parse "red;blue;green" none
; == ["red" "blue" "green"]

Newline

[edit | edit source]
string: {
red
blue  
green
}
parse string none
; == ["red" "blue" "green"]

Leading and trailing whitespaces are ignored

[edit | edit source]
parse " 1 " none
; == ["1"]

A sequence of whitespaces is equivalent to one whitespace

[edit | edit source]
parse "1  2" none
; == ["1" "2"]

Leading common delimiter delimits an empty substring

[edit | edit source]
parse ",1" none
; == ["" "1"]

One trailing common delimiter is ignored

[edit | edit source]
parse "1," none
; == ["1"]

A sequence of common delimiters delimits empty substrings between them

[edit | edit source]
parse "1,,2" none
; == ["1" "" "2"]
parse {"red","blue","green"} none
; == ["red" "blue" "green"]