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

REBOL 3 Functions: parse

Summary

Parses a series according to rules.

parse input rules /all /case

input [series!] - Input series to parse

rules [block! string! char! none!] - Rules to parse by

refinements:

/all - Parses all chars including spaces.

/case - Uses case-sensitive comparison.

Description

The parse function is used to match patterns of values and perform specific actions upon such matches.

Both string! and block! datatypes can be parsed. Parsing of strings matches specific characters or substrings. Parsing of blocks matches specific values, or specific datatypes, or sub-blocks. Whereas most languages provide a method of parsing strings, the parsing of blocks is an important feature of the REBOL language.

The parse function takes two main arguments: an input to be parsed and the rules that are used to parse it. The rules are specified as a block of grammar productions that are to be matched.

There is also a simple parse mode that does not require rules, but takes a string of characters to use for splitting up the input string.

Parse also works in conjunction with bitsets (charset) to specify groups of special characters.

The result returned from a simple parse is a block of values. For rule-based parses, it returns TRUE if the parse succeeded through the end of the input string.

The /ALL refinement indicates that all the characters within a string will be parsed. Otherwise, spaces, tabs, newlines, and other non-printable characters will be treated as spaces.

The /CASE refinement specifies that a string is to be parsed case sensitive.

print parse "divide on spaces" none
divide on spaces
print parse "Harry Haiku, 264 River Rd., Ukiah, 95482" ","
Harry Haiku 264 River Rd. Ukiah 95482
page: read http://hq.rebol.net
parse page [thru <title> copy title to </title>]
print title
Now is REBOL
digits: charset "0123456789"
area-code: ["(" 3 digits ")"]
phone-num: [3 digits "-" 4 digits]
print parse "(707)467-8000" [[area-code | none] phone-num]
true

Related

trim  


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