[Top] [Prev] [Next]

regex - regular expression recognizer

include "regex.m";
regex:= load Regex "/dis/regex.dis";
compile: fn(e: string)       : Re;
execute: fn(x: Re; s: string): (int,int);

Description

The compile function returns a compiled form of the regular expression given in string e, or nil if e is not a valid regular expression.

The execute function matches the compiled regular expression x against string s. It returns indexes of the first character of the longest leftmost match and of the next character beyond the match, or (-1,-1) if no match exists.

The primitives in regular expressions are:
.

matches any character other than newline

\c

matches character c, except \n matches newline

c

matches character c other than one of:

\ . ^ $ ( ) [ ] ? * +

(e)

matches what regular expression e matches

()

matches an empty substring

^

matches an empty substring at the beginning of a string

$

matches an empty substring at the end of a string

[set]

[^set]

matches any character in a set (or its complement), given as a sequence of zero or more items - characters and ranges. An item consists at least of a literal character, not \ or ], or of a character escaped with \. If this is followed by a literal -, it is the lower limit of an inclusive range of Unicode characters. The upper limit is a similarly expressed character after the -.

Repetitions are built from primitives, p, in the following ways.
p

one match to p

p?

zero or one matches to p

p*

zero or more matches to p

p+

one or more matches to p

Regular expressions are built from repetitions, r, and other regular expressions, e1, e2, in the following ways.
r

a repetition

re1

concatenation: a match to r followed by a match to e1

e1|e2

alternation: a match to either e1 or e2; concatenation takes precedence over alternation

Examples


regex:Regex;
(beg, end):=
regex->execute(regex->compile("[ABCb-z]+"), s:="aAbBcCdD");
#s[beg:end] == "AbBcCd"
(beg, end):= 
regex->execute(regex->compile("a*b*"), "bbaabb");
#(beg, end) == (0,2)
re:= regex->compile("(thick)*
                   (chocolate|raspberry)?(topp|fill)ing");



[Top] [Prev] [Next]

infernosupport@lucent.com
Copyright © 1996,Lucent Technologies, Inc. All rights reserved.