[m-dev.] regular expressions

Michael Day mcda at cat.cs.mu.OZ.AU
Wed Dec 15 22:55:37 AEDT 1999


Hi,

if regular expressions were added to the standard library, what kind of
functionality would people like to see? My rather limited experience with
them leads me to propose the following interface:

% this one is fairly obvious, either the string matches the pattern or it
% doesn't. Okay, it may match more than once, not important here.

:- pred match(pattern, string).
:- mode match(in, in) is semidet.

% succeeds if the string matches the pattern, binding the list to any
% backreferences (\1, \2 etc) in the pattern. May succeed more than once.
% Maybe worth having a semidet version that just takes the first match?

:- pred match(pattern, string, list(string)).
:- mode match(in, in, out) is nondet.

% the familiar s/foo/bar/ operator from the text processing language of
% your choice. Always succeeds, simply returning the string if the pattern
% doesn't match anywhere.

:- pred substitute(pattern, string, string).
:- mode substitute(in, in, out) is det.

% a possibility for s/foo/bar/g, that matches *every* instance of the
% pattern. Given that substitute could take other options such as case
% insensitivity it would probably be worth just making it take another
% argument or two, or encode it in the pattern. Similarly with match.

:- pred substitute_global(pattern, string, string).
:- mode substitute_global(in, in, out) is det.


pattern in the above declarations could be just an alias for string, which
would be convenient. But for efficiency as regular expressions could not
be optimised from strings at compile time, it should refer to an abstract
type created with an explicit compile predicate. Or just offer both
versions, as an explicit compile step is annoying if you only want to
match something once.

My primary question though is related to implementation. Is it customary
for languages to roll their own regular expression functionality or simply
wrap the POSIX library for it? Judging previous perl related comments,
perl style regular expressions would not be welcome here? :)

Michael

--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list