@ciao-lang/ts-ciao-interface
Version:
Simple Ciao interface for node.
75 lines (72 loc) • 18.9 kB
HTML
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><meta name="theme-color" content="#273f79"/><link rel="stylesheet" href="lpdoc.css" type="text/css"/><script type="text/javascript" src="lpdoc.js"></script><title>Pattern (regular expression) matching — The Ciao System v1.22</title></head><body><div class="lpdoc-page fixleftbar"><a href="#" id="sidebar-toggle-button" class="lpdoc-navbutton"><span id="sidebar-button-arrow">☰</span></a><div id="sidebar" class="lpdoc-sidebar"><div style="height: 40px; margin-left: auto; margin-right: auto"><img src="ciao-logo_autofig.png" width=auto height=100%></div><div class="lpdoc-nav"><span class="lpdoc-on-right"><a class="lpdoc-navbutton" href="ExtraLibs.html">↑</a><a class="lpdoc-navbutton" href="glob.html">←</a><a class="lpdoc-navbutton" href="regexp_code.html">→</a><a class="lpdoc-navbutton" href="ciaosearch.html">🔍</a></span><span><a href="ciaofulltoc.html">TOC</a></span></div><hr></hr><ul class="lpdoc-itemize-sectpath"><li><a href="ciao.html">The Ciao System</a> »<br/> </li><li><a href="ExtraLibs.html">PART VIII - Additional libraries</a> »<br/> </li><li><a href=""><strong>Pattern (regular expression) matching</strong></a> ▾<ul><li><a href="regexp_code.html">regexp_code (library)</a></li></ul></li></ul><hr></hr><em>ON THIS PAGE</em><ul><li><a href="#Usage and interface">Usage and interface</a></li><li><a href="#Documentation on internals">Documentation on internals</a></li></ul></div><div class="lpdoc-main"><div id=""><h1>Pattern (regular expression) matching</h1><a class="lpdoc-idx-anchor" href="ciaosearch.html#regexp"></a>
<strong>Author(s):</strong> <a class="lpdoc-idx-anchor" id="The Ciao Development Team" href="ciaosearch.html#The Ciao Development Team">The Ciao Development Team</a>.<p>
This library provides facilities for matching strings and terms against <a class="lpdoc-idx-anchor" id="0" href="ciaosearch.html#patterns"><em>patterns</em></a>. Some <a class="lpdoc-idx-anchor" id="1" href="ciaosearch.html#prolog flags">prolog flags</a> are available to control its behavior.<p><ul> <p><li>The flag <tt><a class="lpdoc-idx-anchor" id="2" href="ciaosearch.html#case_insensitive">case_insensitive</a></tt> controls whether to perform case-insensitive (<tt>on</tt>) or case-sensitive (<tt>off</tt>) match. The default value is <tt>off</tt>.<p><li>There is a syntax facility that allows using this regular expression matching in a similar way to unification. You can use <tt>=~ "regexp"</tt> as an argument of a predicate, and then that argument must match with the regexp. For example: <pre class="lpdoc-codeblock"> pred ( =~ "ab*c", B) :- ...
</pre> <p>is equivalent to<p><pre class="lpdoc-codeblock"> pred (X,B) :- match_posix("ab*c",X,R), ...
</pre> <p>Two additional flags control this matching. The first one is <tt><a class="lpdoc-idx-anchor" id="3" href="ciaosearch.html#format">format</a></tt>. Its values are <tt>shell</tt>, <tt>posix</tt>, <tt>list</tt> and <tt>pred</tt>. Their effect is as if changing in the example above the call to <a class="lpdoc-idx-anchor" id="4" href="ciaosearch.html#match_posix/3"><tt>match_posix/3</tt></a> by a call to, respectively, <tt>match_shell/2</tt>, <tt>match_posix/3</tt>, <tt>match_struct/3</tt>, and <tt>match_pred/3</tt>. The default value is <tt>posix</tt>. The other prolog flag is <tt><a class="lpdoc-idx-anchor" id="5" href="ciaosearch.html#exact">exact</a></tt>. Its values are <tt>on</tt> and <tt>off</tt>. The <tt>off</tt> value means replacing in the example <tt>R</tt> with <tt>[]</tt>. If the value is <tt>on</tt>, then <tt>R</tt> is a variable. The default value is <tt>on</tt>.<p></ul><br/><div id="Usage and interface"><h2>Usage and interface</h2><div class="lpdoc-cartouche"><ul><li><strong>Library usage:</strong><br/><tt>:- use_package(regexp).</tt>
or
<tt>:- module(...,...,[regexp]).</tt><li><strong>New operators defined:</strong><br/><a class="lpdoc-idx-anchor" id="6" href="ciaosearch.html#=~/1"><tt>=~/1</tt></a> [200,fy].
<li><strong>Implicit imports:</strong><br/><ul class="lpdoc-itemize-minus"><li><em>System library modules:</em><br/><a class="lpdoc-idx-anchor" id="7" href="regexp_code.html"><tt>regexp_code</tt></a>.
<li><em>Packages:</em><br/><a class="lpdoc-idx-anchor" id="8" href="ciaosearch.html#prelude"><tt>prelude</tt></a>, <a class="lpdoc-idx-anchor" id="9" href="ciaosearch.html#initial"><tt>initial</tt></a>, <a class="lpdoc-idx-anchor" id="10" href="condcomp_doc.html"><tt>condcomp</tt></a>, <a class="lpdoc-idx-anchor" id="11" href="assertions_doc.html"><tt>assertions</tt></a>, <a class="lpdoc-idx-anchor" id="12" href="ciaosearch.html#assertions/assertions_basic"><tt>assertions/assertions_basic</tt></a>.
</ul></ul></div></div><div id="Documentation on internals"><h2>Documentation on internals</h2><div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="match_shell/3" href="ciaosearch.html#match_shell/3">match_shell/3</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>match_shell(Exp,IN,Rest)</tt>
</span><p>Matches <span class="lpdoc-var">IN</span> against <span class="lpdoc-var">Exp</span>. <span class="lpdoc-var">Rest</span> is the longest remainder of the string after the match. For example, <tt>match_shell("??*","foo.pl",Tail)</tt> succeeds, instantiating <tt>Tail</tt> to <tt>"o.pl"</tt>.</p><ul class="lpdoc-itemize-minus"><li><em>The following properties should hold at call time:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="13" href="regexp_code.html#shell_regexp/1"><tt>regexp_code:shell_regexp/1</tt></a>)</span><span><span class="lpdoc-var">Exp</span> is a shell regular expression to match against.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="14" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">IN</span> is a string (a list of character codes).
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="15" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">Rest</span> is a string (a list of character codes).
</span>
</ul></div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="match_shell/2" href="ciaosearch.html#match_shell/2">match_shell/2</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>match_shell(Exp,IN)</tt>
</span><p>Matches completely <span class="lpdoc-var">IN</span> (no tail can remain unmatched) against <span class="lpdoc-var">Exp</span> similarly to <a class="lpdoc-idx-anchor" id="16" href="#match_shell/3"><tt>match_shell/3</tt></a>.</p><ul class="lpdoc-itemize-minus"><li><em>The following properties should hold at call time:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="17" href="regexp_code.html#shell_regexp/1"><tt>regexp_code:shell_regexp/1</tt></a>)</span><span><span class="lpdoc-var">Exp</span> is a shell regular expression to match against.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="18" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">IN</span> is a string (a list of character codes).
</span>
</ul></div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="match_posix/2" href="ciaosearch.html#match_posix/2">match_posix/2</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>match_posix(Exp,IN)</tt>
</span><p>Matches completely <span class="lpdoc-var">IN</span> (no tail can remain unmatched) against <span class="lpdoc-var">Exp</span> similarly to <a class="lpdoc-idx-anchor" id="19" href="ciaosearch.html#match_posix/3"><tt>match_posix/3</tt></a>.</p><ul class="lpdoc-itemize-minus"><li><em>The following properties should hold at call time:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="20" href="regexp_code.html#shell_regexp/1"><tt>regexp_code:shell_regexp/1</tt></a>)</span><span><span class="lpdoc-var">Exp</span> is a shell regular expression to match against.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="21" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">IN</span> is a string (a list of character codes).
</span>
</ul></div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="match_posix/4" href="ciaosearch.html#match_posix/4">match_posix/4</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>match_posix(Exp,In,Match,Rest)</tt>
</span><p></p><ul class="lpdoc-itemize-minus"><li><em>The following properties should hold at call time:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="22" href="regexp_code.html#shell_regexp/1"><tt>regexp_code:shell_regexp/1</tt></a>)</span><span><span class="lpdoc-var">Exp</span> is a shell regular expression to match against.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="23" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">In</span> is a string (a list of character codes).
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="24" href="basic_props.html#list/2"><tt>basic_props:list/2</tt></a>)</span><span><span class="lpdoc-var">Match</span> is a list of <span class="lpdoc-var">string</span>s.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="25" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">Rest</span> is a string (a list of character codes).
</span>
</ul></div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="match_posix_rest/3" href="ciaosearch.html#match_posix_rest/3">match_posix_rest/3</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>match_posix_rest(Exp,IN,Rest)</tt>
</span><p>Matches <span class="lpdoc-var">IN</span> against <span class="lpdoc-var">Exp</span>. <span class="lpdoc-var">Tail</span> is the remainder of the string after the match. For example, <tt>match_posix("ab*c","abbbbcdf",Tail)</tt> succeeds, instantiating <tt>Tail</tt> to <tt>"df"</tt>.</p><ul class="lpdoc-itemize-minus"><li><em>The following properties should hold at call time:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="26" href="regexp_code.html#posix_regexp/1"><tt>regexp_code:posix_regexp/1</tt></a>)</span><span><span class="lpdoc-var">Exp</span> is a posix regular expression to match against.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="27" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">IN</span> is a string (a list of character codes).
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="28" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">Rest</span> is a string (a list of character codes).
</span>
</ul></div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="match_posix_matches/3" href="ciaosearch.html#match_posix_matches/3">match_posix_matches/3</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>match_posix_matches(Exp,IN,Matches)</tt>
</span><p>Matches completely <span class="lpdoc-var">IN</span> against <span class="lpdoc-var">Exp</span>. <span class="lpdoc-var">Exp</span> can contain <em>anchored expressions</em> of the form \(<tt>regexp</tt>\). <span class="lpdoc-var">Matches</span> will contain a list of the <em>anchored expression</em> which were matched on success. Note that since POSIX expressions are being read inside a string, backslashes will have to be doubled. For example,<p><pre class="lpdoc-codeblock">?- 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
</pre></p><ul class="lpdoc-itemize-minus"><li><em>The following properties should hold at call time:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="29" href="regexp_code.html#shell_regexp/1"><tt>regexp_code:shell_regexp/1</tt></a>)</span><span><span class="lpdoc-var">Exp</span> is a shell regular expression to match against.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="30" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">IN</span> is a string (a list of character codes).
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="31" href="basic_props.html#list/2"><tt>basic_props:list/2</tt></a>)</span><span><span class="lpdoc-var">Matches</span> is a list of <span class="lpdoc-var">string</span>s.
</span>
</ul></div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="match_struct/4" href="ciaosearch.html#match_struct/4">match_struct/4</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>match_struct(Exp,IN,Rest,Tail)</tt>
</span><p>Matches <span class="lpdoc-var">IN</span> against <span class="lpdoc-var">Exp</span>. <span class="lpdoc-var">Tail</span> is the remainder of the list of atoms <span class="lpdoc-var">IN</span> after the match. For example, <tt>match_struct([a,*(b),c],[a,b,b,b,c,d,e],Tail)</tt> succeeds, instantiating <tt>Tail</tt> to <tt>[d,e]</tt>.</p><ul class="lpdoc-itemize-minus"><li><em>Call and exit should be compatible with:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="32" href="regexp_code.html#struct_regexp/1"><tt>regexp_code:struct_regexp/1</tt></a>)</span><span><span class="lpdoc-var">Exp</span> is a struct regular expression to match against.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="33" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">IN</span> is a string (a list of character codes).
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="34" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">Rest</span> is a string (a list of character codes).
</span>
</ul></div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="match_pred/2" href="ciaosearch.html#match_pred/2">match_pred/2</a></div><div class="lpdoc-deftext">No further documentation available for this predicate.</div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="replace_first/4" href="ciaosearch.html#replace_first/4">replace_first/4</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>replace_first(IN,Old,New,Resul)</tt>
</span><p>Replace the first occurrence of the <span class="lpdoc-var">Old</span> by <span class="lpdoc-var">New</span> in <span class="lpdoc-var">IN</span> and copy the result in <span class="lpdoc-var">Resul</span>.</p><ul class="lpdoc-itemize-minus"><li><em>The following properties should hold at call time:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="35" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">IN</span> is a string (a list of character codes).
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="36" href="regexp_code.html#posix_regexp/1"><tt>regexp_code:posix_regexp/1</tt></a>)</span><span><span class="lpdoc-var">Old</span> is a posix regular expression to match against.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="37" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">New</span> is a string (a list of character codes).
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="38" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">Resul</span> is a string (a list of character codes).
</span>
</ul></div></div><p>
<div><div class="lpdoc-defname"><span class="lpdoc-predtag">PREDICATE</span><a class="lpdoc-idx-anchor" id="replace_all/4" href="ciaosearch.html#replace_all/4">replace_all/4</a></div><div class="lpdoc-deftext"><p><span class="lpdoc-usage-header">Usage:</span><span class="lpdoc-usage-decl"><tt>replace_all(IN,Old,New,Resul)</tt>
</span><p>Replace all occurrences of the <span class="lpdoc-var">Old</span> by <span class="lpdoc-var">New</span> in <span class="lpdoc-var">IN</span> and copy the result in <span class="lpdoc-var">Resul</span>.</p><ul class="lpdoc-itemize-minus"><li><em>The following properties should hold at call time:</em><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="39" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">IN</span> is a string (a list of character codes).
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="40" href="regexp_code.html#posix_regexp/1"><tt>regexp_code:posix_regexp/1</tt></a>)</span><span><span class="lpdoc-var">Old</span> is a posix regular expression to match against.
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="41" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">New</span> is a string (a list of character codes).
</span><br/><span class="lpdoc-on-right"> (<a class="lpdoc-idx-anchor" id="42" href="basic_props.html#string/1"><tt>basic_props:string/1</tt></a>)</span><span><span class="lpdoc-var">Resul</span> is a string (a list of character codes).
</span>
</ul></div></div><p>
</div></div><div class="lpdoc-footer">Generated with LPdoc using Ciao</div></div><div class="lpdoc-clearer"></div></div></body></html>