REGEX(2) REGEX(2)
NAME
regex - regular expression recognizer module
SYNOPSIS
include "regex.m";
regex := load Regex Regex->PATH;
compile: fn(e: string, flag: int): (Re, string);
execute: fn(x: Re; s: string): array of (int,int);
executese: fn(x: Re, s: string, se: (int, int), bol: int, eol: int):
array of (int, int);
DESCRIPTION
Regular expressions are defined by regexp(6). Compile
returns (as the first element of the result tuple) a com-
piled form of the regular expression given in string e. If e
is not a valid regular expression, compile returns the tuple
(nil, diag) where diag is a diagnostic string. The effect
of flag is described below.
Execute matches the compiled regular expression x against
string s. It returns nil on no match, otherwise it returns
an array. The element with index 0 gives the character
positions of the first character of some longest leftmost
match and the first character beyond the match. If the com-
pilation flag was 0, there are no more elements. If flag
was 1, there is one element for each pair of parentheses in
the regular expression, counting left parentheses left to
right starting at 1. The nth element gives the position of
the last match to the nth parenthesized subexpression, or
(-1,-1) if the subexpression does not participate in the
overall match.
Executese is similar to execute but allows the start and end
points in the string to be specified, as tuple se: (start ,
end), where start is the index in s of the initial character
to be matched, and end is the index in s of the first char-
acter beyond the substring to be matched (and can be the
length of s). If bol is non-zero, the initial character is
at the beginning of a line, allowing an initial match by the
regular expression operator `^'; if eol is non-zero, the
last character is at the end of a line, allowing a match by
the operator `$'.
EXAMPLES
(re, nil) := regex->compile("(thick )*(chocolate |raspberry )?"+
"(topp|fill)ing", 0);
(re, nil) := regex->compile("[ABCb-z]+", 0);
a := regex->execute(re, s:="aAbBcCdD");
Page 1 Plan 9 (printed 10/25/25)
REGEX(2) REGEX(2)
(beg, end) := a[0];
s[beg:end] == "AbBcCd";
(re, nil) := regex->compile("a*b*", 0);
a := regex->execute(re, "bbaabb");
(beg, end) := a[0];
(beg, end) == (0,2);
SOURCE
/appl/lib/regex.b
SEE ALSO
regexp(6)
Page 2 Plan 9 (printed 10/25/25)