Pattern (regular expression) matching

Author(s): The Ciao Development Team.

This library provides facilities for matching strings and terms against patterns. Some prolog flags are available to control its behavior.

  • The flag case_insensitive controls whether to perform case-insensitive (on) or case-sensitive (off) match. The default value is off.

  • There is a syntax facility that allows using this regular expression matching in a similar way to unification. You can use =~ "regexp" as an argument of a predicate, and then that argument must match with the regexp. For example:
       pred ( =~ "ab*c", B) :- ...

    is equivalent to

          pred (X,B) :- match_posix("ab*c",X,R), ...

    Two additional flags control this matching. The first one is format. Its values are shell, posix, list and pred. Their effect is as if changing in the example above the call to match_posix/3 by a call to, respectively, match_shell/2, match_posix/3, match_struct/3, and match_pred/3. The default value is posix. The other prolog flag is exact. Its values are on and off. The off value means replacing in the example R with []. If the value is on, then R is a variable. The default value is on.


Usage and interface

Documentation on internals

PREDICATEmatch_shell/3

Usage:match_shell(Exp,IN,Rest)

Matches IN against Exp. Rest is the longest remainder of the string after the match. For example, match_shell("??*","foo.pl",Tail) succeeds, instantiating Tail to "o.pl".

PREDICATEmatch_shell/2

Usage:match_shell(Exp,IN)

Matches completely IN (no tail can remain unmatched) against Exp similarly to match_shell/3.

PREDICATEmatch_posix/2

Usage:match_posix(Exp,IN)

Matches completely IN (no tail can remain unmatched) against Exp similarly to match_posix/3.

PREDICATEmatch_posix/4

Usage:match_posix(Exp,In,Match,Rest)

Usage:match_posix_rest(Exp,IN,Rest)

Matches IN against Exp. Tail is the remainder of the string after the match. For example, match_posix("ab*c","abbbbcdf",Tail) succeeds, instantiating Tail to "df".

Usage:match_posix_matches(Exp,IN,Matches)

Matches completely IN against Exp. Exp can contain anchored expressions of the form \(regexp\). Matches will contain a list of the anchored expression which were matched on success. Note that since POSIX expressions are being read inside a string, backslashes will have to be doubled. For example,

?- match_posix_matches("\(aa|bb\)\(bb|aa\)", "bbaa", M).
M = ["bb","aa"] ? ;
no

?- match_posix_matches("\(aa|bb\)\(bb|aa\)", "aabb", M).
M = ["aa","bb"] ? ;
no

Usage:match_struct(Exp,IN,Rest,Tail)

Matches IN against Exp. Tail is the remainder of the list of atoms IN after the match. For example, match_struct([a,*(b),c],[a,b,b,b,c,d,e],Tail) succeeds, instantiating Tail to [d,e].

PREDICATEmatch_pred/2
No further documentation available for this predicate.

Usage:replace_first(IN,Old,New,Resul)

Replace the first occurrence of the Old by New in IN and copy the result in Resul.

PREDICATEreplace_all/4

Usage:replace_all(IN,Old,New,Resul)

Replace all occurrences of the Old by New in IN and copy the result in Resul.