REBOL Programming/Language Features/Parse/Simple splitting

From Wikibooks, open books for an open world
Jump to: navigation, search

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]

Empty string[edit]

parse "" none
; == []

No delimiters in the input string[edit]

parse "redbluegreen" none
; == ["redbluegreen"]

Space[edit]

parse "red blue green" none
; == ["red" "blue" "green"]

Comma[edit]

parse "red,blue,green" none
; == ["red" "blue" "green"]

Tab[edit]

parse "red^-blue^-green" none
; == ["red" "blue" "green"]

Semicolon[edit]

parse "red;blue;green" none
; == ["red" "blue" "green"]

Newline[edit]

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

Leading and trailing whitespaces are ignored[edit]

parse " 1 " none
; == ["1"]

A sequence of whitespaces is equivalent to one whitespace[edit]

parse "1  2" none
; == ["1" "2"]

Leading common delimiter delimits an empty substring[edit]

parse ",1" none
; == ["" "1"]

One trailing common delimiter is ignored[edit]

parse "1," none
; == ["1"]

A sequence of common delimiters delimits empty substrings between them[edit]

parse "1,,2" none
; == ["1" "" "2"]

CSV[edit]

parse {"red","blue","green"} none
; == ["red" "blue" "green"]