REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 6-Feb-2009 Edit History  

REBOL 3 Concepts: Parsing: Simple Splitting

Pending Revision

This document was written for R2 and has yet to be revised for R3.

A simple form of parse is for splitting strings:

parse string none

The parse function splits the input argument, string!, into a block of multiple strings, breaking each string wherever it encounters a delimiter, such as a space, tab, newline, comma, or semicolon. The none! argument indicates that no other delimiters other than these. For example:

probe parse "The trip will take 21 days" none
["The" "trip" "will" "take" "21" "days"]

Similarly,

probe parse "here there,everywhere; ok" none
["here" "there" "everywhere" "ok"]

In the example above, notice that the commas and semicolons have been removed from the resulting strings.

You can specify your own delimiters in the second argument to parse. For example, the following code parses a telephone number with dash (-) delimiters:

probe parse "707-467-8000" "-"
["707" "467" "8000"]

The next example uses equal (=) and double quote (") as the delimiters:

probe parse <IMG SRC="test.gif" WIDTH="123"> {="}
["IMG" "SRC" "test.gif" "WIDTH" "123"]

The next example parses a string based on commas only; any other delimiters are ignored. Consequently, the spaces within the strings are not removed:

Normally when you parse strings, any whitespace (space, tab, lines) are automatically processed as delimiters. To avoid that action, you can use the /any refinement. Compare these two examples:

parse "Test This" ""
["Test" "This"]
parse/all "Test This" ""
["Test This"]

In the second, you can see that the space was not treated as a delimiter.

Here is another example:

probe parse/all "Harry, 1011 Main St., Ukiah" ","
["Harry" " 1011 Main St." " Ukiah"]

You can also parse strings that contain null characters as separators (such as certain types of data files):

parse/all nulled-string "^(null)"


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin