1 Regular Expressions

The regex module implements an interface to the POSIX regex library and builds some higher level facilities on top of it. The module may be imported as follows:

import Regex at 'x-oz://contrib/regex'


{Regex.is +X ?B}

Returns true iff X is a regex object.


{Regex.make +PAT ?RE}

Creates regex object RE from virtual string pattern PAT. Note that Regex.make cannot be used in (subordinated) spaces.


{Regex.search +RE +TXT ?MATCH}

Returns the next MATCH of regex RE in virtual string TXT, or false if there is no such match. RE is also permitted to be a virtual string pattern, in which case it is automatically compiled into a regex object.

A match is a record with integer features: one for each group in the pattern and also feature 0 for the whole match. The value on each feature is a pair I#J of start and end indices into TXT. If TXT is a byte string, you can simply invoke {ByteString.slice TXT I J} to extract the match.


{Regex.group +N +MATCH +TXT ?GROUP}

Return the substring GROUP matched by the N group of MATCH in virtual string TXT. MATCH should be the result of calling Regex.search on TXT.


{Regex.groups +MATCH +TXT ?GROUPS}

Returns the record GROUPS, with label group, of substrings of TXT corresponding to the groups of MATCH. Group 0 (the whole match) is not included.


{Regex.allMatches +RE +TXT ?MATCHES}

Returns the list of all MATCHES of regular expression RE in virtual string TXT. RE should not be anchored.


{Regex.forAll +RE +TXT +P}

Applies the 1 argument procedure P to every match of RE in TXT.


{Regex.map +RE +TXT +F ?RESULTS}

Applies the 1 argument function F to every match of RE in TXT and returns the corresponding list of RESULTS.


{Regex.foldR +RE +TXT +F INIT ?RESULT}


{Regex.foldL +RE +TXT +F INIT ?RESULT}

The usual reduction procedure (see List module).


{Regex.split +RE +TXT ?STRINGS}

Splits the input TXT at every match of separator RE, and returns the resulting list of strings.


{Regex.compile +PAT +CFLAGS ?RE}

This is the more complicated version of Regex.make. The additional CFLAGS argument further parametrizes the regex compilation process. It is either an atom or a list of atoms, from the set: extended, icase, nosub, newline. The default is [extended newline]. See the man page for regcomp for further details.


{Regex.execute +RE +TXT +IDX +EFLAGS ?MATCH}

This is the more complicated version of Regex.search. Integer IDX is the offset at which to start the search in +TXT. EFLAGS further specify how to search: it is an atom or list of atoms, from the set: notbol, noteol. The default is nil. See the man page for regexec for further details.


{Regex.cflags.set +SPEC}

{Regex.cflags.get ?SPEC}

Set or get the current CFLAGS defaults, e. g. used by Regex.make.


{Regex.eflags.set +SPEC}

{Regex.eflags.get ?SPEC}

Set or get the current EFLAGS defaults, e. g. used by Regex.search.


{Regex.replace +TXT +RE +FUN ?RES}

Replace every occurrence of RE in TXT with the result of applying FUN to the current TXT and the current match.


{Regex.replaceRegion +TXT +LO +HI +RE +FUN ?RES}

Same as above, but only operate on the region starting at LO inclusive and ending at HI exclusive.

Denys Duchier and Leif Kornstaedt
Version 1.4.0 (20080702)