reg-expr->proc

Name

reg-expr->proc -- Compiles a regular expression

Type: function

Synopsis

reg-expr->proc( expr, proc list);

Arguments

expr

A regular expression.

Return Values

proc

An instance of <function>.

list

A list describing the proc's return values.

Description

This function compiles a regular expression into a callable object. The valid forms of regular expressions are given in the table at the beginning of this section.

The second return value, list, describes the values that will be returned from proc when it finds a match. The first element is always the symbol substring, indicating that the proc returns the start and end index of the substring that matched. The remaining elements are the names from each occurrence of a let form in expr, indicating the strings returned for each use of let. let's without names are assigned integer names 1, 2, 3, ... [really, save is the appopriate choice for unnamed let's]

(reg-expr->proc '(seq (+ space) (let w (+ alpha)))) #[<procedure>] (substring w)

If the proc finds a match, it return two values -- the start and end index of the substring that matched -- plus one string for each let construct in the regular expression.

Since the proc returns multiple values, the bind construct may be used to capture the multiple values.

(define q (reg-expr->proc '(seq (+ #\*) (save (+ alpha)))))

(define str "...***hello there")

(q str) 3 11 "hello"

(substring str 3 11) "***hello"

(bind ((s e w (q str))) w) "hello"