Regular Expressions

RScheme comes with a regular expression facility, accessible as the regex module. This module provides a regular expression compiler; regular expressions in a structured scheme-like syntax are compiled into functions which know how to search for their pattern.

Compiling a regular expression is relatively expensive, so it is best to factor out the operation whenever convenient.

The returned procedure takes one or two arguments. It's first argument is the string to search for itself in. The second (optional) argument is the offset at which to start looking, which defaults to 0.

Regular expressions are composed of the following operators:

Table 8-1. Regular Expression Operators

<char>#\amatch the specified character
<string>"foo"match the given sequence of characters
<symbol>spacematch a character in the named character class. Valid character classes are: any digit alpha uppercase lowercase hex-digit printable space
eoseosmatch the end of the string
(or P1 ...)(or #\a space)match any one of the given patterns (may be any pattern, not just characters)
(seq P1 ...)(seq #\a #\b)match the sequence of patterns
(+ P)(+ space)match one or more occurrences of P
(* P)(* alpha)match zero or more occurrences of P
(? P)(? #\x)match zero or one P's
(range C1 C2)(range #\a #\z)match a character in the range C1..C2 (C1 and C2 must be literal characters)
(not P)(not space)match a character not matched by P (P must be a primitive pattern (i.e., a character pattern, character class, or a or or not of primitive patterns))
(save P)(save (+ (not #\,)))match P and return the matched substring as an additional value
(let N P)(let id (+ alpha))match P and return the matched substring as an additional value, using N as the name of the field.
(prefix P)(prefix #\a)match P if it is at the beginning of the string
(suffix P)(suffix #\.)match P if it is at the end of the string
(entire P)(entire "foo")match P if it is the entire string


Table of Contents
reg-expr->proc -- Compiles a regular expression
unformat->proc -- Constructs an "unformat" procedure


The following examples illustrate the basic behavior of the regular expression module.

Example 8-1. Regular Expression Examples

(define p (reg-expr->proc '(+ alpha)))

(p "1 2 Buckle my shoe") 4 10

(define p2 (reg-expr->proc '(let x (+ alpha))))

(p2 "5 6 pick up sticks") 4 8 "pick"

(p2 "5 6 pick up sticks" 8) 9 11 "up"