Debian Codegraph @

cl-ppcre/doc/index.html

raw
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <html>
3
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6 <title>CL-PPCRE - Portable Perl-compatible regular expressions for Common Lisp</title>
7 <style type="text/css">
8 pre { padding:5px; background-color:#e0e0e0 }
9 h3, h4 { text-decoration: underline; }
10 a { text-decoration: none; padding: 1px 2px 1px 2px; }
11 a:visited { text-decoration: none; padding: 1px 2px 1px 2px; }
12 a:hover { text-decoration: none; padding: 1px 1px 1px 1px; border: 1px solid #000000; }
13 a:focus { text-decoration: none; padding: 1px 2px 1px 2px; border: none; }
14 a.none { text-decoration: none; padding: 0; }
15 a.none:visited { text-decoration: none; padding: 0; }
16 a.none:hover { text-decoration: none; border: none; padding: 0; }
17 a.none:focus { text-decoration: none; border: none; padding: 0; }
18 a.noborder { text-decoration: none; padding: 0; }
19 a.noborder:visited { text-decoration: none; padding: 0; }
20 a.noborder:hover { text-decoration: none; border: none; padding: 0; }
21 a.noborder:focus { text-decoration: none; border: none; padding: 0; }
22 pre.none { padding:5px; background-color:#ffffff }
23 </style>
24 <meta name="description" content="Fast and portable perl-compatible regular expressions for Common Lisp.">
25 </head>
26
27 <body bgcolor=white>
28
29 <h2>CL-PPCRE - Portable Perl-compatible regular expressions for Common Lisp</h2>
30
31 <blockquote>
32 <br>&nbsp;<br><h3>Abstract</h3>
33
34 CL-PPCRE is a portable regular expression library for Common Lisp
35 which has the following features:
36
37 <ul>
38
39 <li>It is <b>compatible with Perl</b>.
40
41 <li>It is pretty <b>fast</b>.
42
43 <li>It is <b>portable</b> between ANSI-compliant Common Lisp
44 implementations.
45
46 <li>It is <b>thread-safe</b>.
47
48 <li>In addition to specifying regular expressions as strings like in
49 Perl you can also use <a
50 href="#create-scanner2"><b>S-expressions</b></a>.
51
52 <li>It comes with a <a
53 href="http://www.opensource.org/licenses/bsd-license.php"><b>BSD-style
54 license</b></a> so you can basically do with it whatever you want.
55
56 </ul>
57
58 CL-PPCRE has been used successfully in various applications like <a
59 href="http://nostoc.stanford.edu/Docs/">BioBike</a>,
60 <a href="http://clutu.com/">clutu</a>,
61 <a
62 href="http://www.hpc.unm.edu/~download/LoGS/">LoGS</a>, <a href="http://cafespot.net/">CafeSpot</a>, <a href="http://www.eboy.com/">Eboy</a>, or <a
63 href="http://weitz.de/regex-coach/">The Regex Coach</a>.
64
65 <p>
66 <font color=red>Download shortcut:</font> <a href="http://weitz.de/files/cl-ppcre.tar.gz">http://weitz.de/files/cl-ppcre.tar.gz</a>.
67
68 </blockquote>
69
70 <br>&nbsp;<br><h3><a class=none name="contents">Contents</a></h3>
71 <ol>
72 <li><a href="#install">Download and installation</a>
73 <li><a href="#mail">Support and mailing lists</a>
74 <li><a href="#dict">The CL-PPCRE dictionary</a>
75 <ol>
76 <li><a href="#scanning">Scanning</a>
77 <ol>
78 <li><a href="#create-scanner"><code>create-scanner</code></a> (for Perl regex strings)
79 <li><a href="#create-scanner2"><code>create-scanner</code></a> (for parse trees)
80 <li><a href="#scan"><code>scan</code></a>
81 <li><a href="#scan-to-strings"><code>scan-to-strings</code></a>
82 <li><a href="#register-groups-bind"><code>register-groups-bind</code></a>
83 <li><a href="#do-scans"><code>do-scans</code></a>
84 <li><a href="#do-matches"><code>do-matches</code></a>
85 <li><a href="#do-matches-as-strings"><code>do-matches-as-strings</code></a>
86 <li><a href="#do-register-groups"><code>do-register-groups</code></a>
87 <li><a href="#all-matches"><code>all-matches</code></a>
88 <li><a href="#all-matches-as-strings"><code>all-matches-as-strings</code></a>
89 </ol>
90 <li><a href="#splitting">Splitting and replacing</a>
91 <ol>
92 <li><a href="#split"><code>split</code></a>
93 <li><a href="#regex-replace"><code>regex-replace</code></a>
94 <li><a href="#regex-replace-all"><code>regex-replace-all</code></a>
95 </ol>
96 <li><a href="#modify">Modifying scanner behaviour</a>
97 <ol>
98 <li><a href="#*property-resolver*"><code>*property-resolver*</code></a>
99 <li><a href="#parse-tree-synonym"><code>parse-tree-synonym</code></a>
100 <li><a href="#define-parse-tree-synonym"><code>define-parse-tree-synonym</code></a>
101 <li><a href="#*regex-char-code-limit*"><code>*regex-char-code-limit*</code></a>
102 <li><a href="#*use-bmh-matchers*"><code>*use-bmh-matchers*</code></a>
103 <li><a href="#*optimize-char-classes*"><code>*optimize-char-classes*</code></a>
104 <li><a href="#*allow-quoting*"><code>*allow-quoting*</code></a>
105 <li><a href="#*allow-named-registers*"><code>*allow-named-registers*</code></a>
106 </ol>
107 <li><a href="#misc">Miscellaneous</a>
108 <ol>
109 <li><a href="#parse-string"><code>parse-string</code></a>
110 <li><a href="#create-optimized-test-function"><code>create-optimized-test-function</code></a>
111 <li><a href="#quote-meta-chars"><code>quote-meta-chars</code></a>
112 <li><a href="#regex-apropos"><code>regex-apropos</code></a>
113 <li><a href="#regex-apropos-list"><code>regex-apropos-list</code></a>
114 </ol>
115 <li><a href="#conditions">Conditions</a>
116 <ol>
117 <li><a href="#ppcre-error"><code>ppcre-error</code></a>
118 <li><a href="#ppcre-invocation-error"><code>ppcre-invocation-error</code></a>
119 <li><a href="#ppcre-syntax-error"><code>ppcre-syntax-error</code></a>
120 <li><a href="#ppcre-syntax-error-string"><code>ppcre-syntax-error-string</code></a>
121 <li><a href="#ppcre-syntax-error-pos"><code>ppcre-syntax-error-pos</code></a>
122 </ol>
123 </ol>
124 <li><a href="#unicode">Unicode properties</a>
125 <ol>
126 <li><a href="#unicode-property-resolver"><code>unicode-property-resolver</code></a>
127 </ol>
128 <li><a href="#filters">Filters</a>
129 <li><a href="#perl">Compatibility with Perl</a>
130 <ol>
131 <li><a href="#empty">Empty strings instead of <code>undef</code> in <code>$1</code>, <code>$2</code>, etc.</a>
132 <li><a href="#scope">Strange scoping of embedded modifiers</a>
133 <li><a href="#inconsistent">Inconsistent capturing of <code>$1</code>, <code>$2</code>, etc.</a>
134 <li><a href="#lookaround">Captured groups not available outside of look-aheads and look-behinds</a>
135 <li><a href="#order">Alternations don't always work from left to right</a>
136 <li><a href="#uprops">Different names for Unicode properties</a>
137 <li><a href="#mac"><code>&quot;\r&quot;</code> doesn't work with MCL</a>
138 <li><a href="#alpha">What about <code>&quot;\w&quot;</code>?</a>
139 </ol>
140 <li><a href="#bugs">Bugs and problems</a>
141 <ol>
142 <li><a href="#quote"><code>&quot;\Q&quot;</code> doesn't work, or does it?</a>
143 <li><a href="#backslash">Backslashes may confuse you...</a>
144 </ol>
145 <li><a href="#allegro">AllegroCL compatibility mode</a>
146 <li><a href="#blabla">Hints, comments, performance considerations</a>
147 <li><a href="#ack">Acknowledgements</a>
148 </ol>
149
150 <br>&nbsp;<br><h3><a name="install" class=none>Download and installation</a></h3>
151
152 CL-PPCRE together with this documentation can be downloaded from <a
153 href="http://weitz.de/files/cl-ppcre.tar.gz">http://weitz.de/files/cl-ppcre.tar.gz</a>. The
154 current version is 2.0.4.
155 <p>
156 CL-PPCRE comes with a system definition
157 for <a href="http://www.cliki.net/asdf">ASDF</a> and you compile and
158 load it in the usual way. There are no dependencies (except that the
159 <a href="#test">test suite</a> which is not needed for normal operation depends
160 on <a href="http://weitz.de/flexi-streams/">FLEXI-STREAMS</a>).
161 <p>
162 The preferred way to install CL-PPCRE is
163 through <a href="http://www.quicklisp.org/" target="_new">Quicklisp</a>:
164 <pre>(ql:quickload :cl-ppcre)</pre>
165 </p>
166 <p>
167 <a class=none name="test">You</a> can run a test suite which tests most aspects of the library with
168 <pre>
169 (asdf:oos 'asdf:test-op :cl-ppcre)
170 </pre>
171 <p>
172 The current development version of CL-PPCRE can be found
173 at <a href="http://bknr.net/trac/browser/trunk/thirdparty">http://bknr.net/trac/browser/trunk/thirdparty</a>.
174 This is the one to send <a href="#mail">patches</a> against. Use at
175 your own risk.
176 <p>
177 Lu&iacute;s Oliveira maintains an
178 unofficial <a href="http://darcs.net/">darcs</a> repository of
179 CL-PPCRE
180 at <a href="http://common-lisp.net/~loliveira/ediware/">http://common-lisp.net/~loliveira/ediware/</a>.
181
182
183 <br>&nbsp;<br><h3><a name="mail" class=none>Support and mailing lists</a></h3>
184
185 For questions, bug reports, feature requests, improvements, or patches
186 please use
187 the <a href="http://common-lisp.net/mailman/listinfo/cl-ppcre-devel">cl-ppcre-devel
188 mailing list</a>. If you want to be notified about future releases,
189 subscribe to
190 the <a href="http://common-lisp.net/mailman/listinfo/cl-ppcre-announce">cl-ppcre-announce
191 mailing list</a>. These mailing lists were made available thanks to
192 the services of <a href="http://common-lisp.net/">common-lisp.net</a>.
193 Terrence Brannon has created a Google group for the list which is
194 at <a href="http://groups.google.com/group/cl-ppcre">http://groups.google.com/group/cl-ppcre</a>.
195 <p>
196 If you want to send patches, please <a href="http://weitz.de/patches.html">read this first</a>.
197
198 <br>&nbsp;<br><h3><a class=none name="dict">The CL-PPCRE dictionary</a></h3>
199
200 <h4><a name="scanning" class=none>Scanning</a></h4>
201
202 <p><br>[Method]
203 <br><a class=none name="create-scanner"><b>create-scanner</b> <i>(string string)<tt>&amp;key</tt> case-insensitive-mode multi-line-mode single-line-mode extended-mode destructive</i> =&gt; <i>scanner, register-names</i></a>
204
205 <blockquote><br> Accepts a string which is a regular expression in
206 Perl syntax and returns a closure which will scan strings for this
207 regular expression. The second value is only returned if <a href="#*allow-named-registers*"><code>*ALLOW-NAMED-REGISTERS*</code></a> is <i>true</i>. It represents a list of strings mapping registers to their respective names - the first element stands for first register, the second element for second register, etc. You have to store this value if you want to map a register number to its name later as <i>scanner</i> doesn't capture any information about register names. If a register isn't named, it has NIL as its name.
208 <p>
209 The mode keyword arguments are equivalent to the
210 <code>&quot;imsx&quot;</code> modifiers in Perl. The
211 <code>destructive</code> keyword will be ignored.
212 <p>
213 The function accepts most of the regex syntax of Perl 5.8 as described
214 in <a href="http://perldoc.perl.org/5.8.8/perlre.html"><code>man
215 perlre</code></a> including extended features like non-greedy
216 repetitions, positive and negative look-ahead and look-behind
217 assertions, &quot;standalone&quot; subexpressions, and conditional
218 subpatterns. The following Perl features are (currently) <b>not</b>
219 supported:
220
221 <ul>
222
223 <li><code>(?{ code })</code> and <code>(??{ code })</code> because
224 they obviously don't make sense in Lisp.
225
226 <li><code>\N{name}</code> (named characters), <code>\x{263a}</code>
227 (wide hex characters), <code>\l</code>, <code>\u</code>,
228 <code>\L</code>, and <code>\U</code>
229 because they're actually not part of Perl's <em>regex</em> syntax - but see <a href="http://weitz.de/cl-interpol/">CL-INTERPOL</a>.
230
231 <li><code>\X</code> (extended Unicode), and <code>\C</code> (single
232 character). But you can of course use all characters
233 supported by your CL implementation.
234
235 <li>Posix character classes like <code>[[:alpha]]</code>.
236 Use <a href="#unicode">Unicode properties</a> instead.
237
238 <li><code>\G</code> for Perl's <code>pos()</code> because we don't have it.
239
240 </ul>
241
242 Note, however, that <code>\t</code>, <code>\n</code>, <code>\r</code>,
243 <code>\f</code>, <code>\a</code>, <code>\e</code>, <code>\033</code>
244 (octal character codes), <code>\x1B</code> (hexadecimal character
245 codes), <code>\c[</code> (control characters), <code>\w</code>,
246 <code>\W</code>, <code>\s</code>, <code>\S</code>, <code>\d</code>,
247 <code>\D</code>, <code>\b</code>, <code>\B</code>, <code>\A</code>,
248 <code>\Z</code>, and <code>\z</code> <b>are</b> supported.
249 <p>
250 Since version 0.6.0, CL-PPCRE also supports Perl's <code>\Q</code> and <code>\E</code> - see <a
251 href="#*allow-quoting*"><code>*ALLOW-QUOTING*</code></a> below. Make sure you also read <a href="#quote">the relevant section</a> in &quot;<a href="#bugs">Bugs and problems</a>.&quot;
252 <p>
253 Since version 1.3.0, CL-PPCRE offers support for <a href="http://www.franz.com/support/documentation/7.0/doc/regexp.htm#regexp-new-capturing-2">AllegroCL's</a> <code>(?&lt;name&gt;"&lt;regex&gt;")</code> named registers and <code>\k&lt;name&gt;</code> back-references syntax, have a look at <a href="#*allow-named-registers*"><code>*ALLOW-NAMED-REGISTERS*</code></a> for details.
254 <p>
255 Since version 2.0.0, CL-PPCRE
256 supports <a href="#*property-resolver*">named properties</a>
257 (<code>\p</code> and <code>\P</code>), but only the long form with
258 braces is supported, i.e. <code>\p{Letter}</code>
259 and <code>\p{L}</code> will work while <code>\pL</code> won't.
260 <p>
261 The keyword arguments are just for your
262 convenience. You can always use embedded modifiers like
263 <code>&quot;(?i-s)&quot;</code> instead.</blockquote>
264
265 <p><br>[Method]
266 <br><a class=none name="create-scanner"><b>create-scanner</b> <i>(function function)<tt>&amp;key</tt> case-insensitive-mode multi-line-mode single-line-mode extended-mode destructive</i> =&gt; <i>scanner</i></a>
267 <blockquote><br> In this case <code><i>function</i></code> should be a
268 scanner returned by another invocation
269 of <code>CREATE-SCANNER</code>. It will be returned as is. You can't
270 use any of the keyword arguments because the scanner has already been
271 created and is immutable.
272 </blockquote>
273
274 <p><br>[Method]
275 <br><a class=none name="create-scanner2"><b>create-scanner</b> <i>(parse-tree t)<tt>&amp;key</tt> case-insensitive-mode multi-line-mode single-line-mode extended-mode destructive</i> =&gt; <i>scanner, register-names</i></a>
276 <blockquote><br>
277 This is similar to <a
278 href="#create-scanner"><code>CREATE-SCANNER</code></a> for regex strings above but
279 accepts a <em>parse tree</em> as its first argument. A parse tree is an S-expression
280 conforming to the following syntax:
281
282 <ul>
283
284 <li>Every string and character is a parse tree and is treated
285 <em>literally</em> as a part of the regular expression,
286 i.e. parentheses, brackets, asterisks and such aren't special.
287
288 <li>The symbol <code>:VOID</code> is equivalent to the empty string.
289
290 <li>The symbol <code>:EVERYTHING</code> is equivalent to Perl's dot,
291 i.e it matches everything (except maybe a newline character depending
292 on the mode).
293
294 <li>The symbols <code>:WORD-BOUNDARY</code> and
295 <code>:NON-WORD-BOUNDARY</code> are equivalent to Perl's
296 <code>&quot;\b&quot;</code> and <code>&quot;\B&quot;</code>.
297
298 <li>The symbols <code>:DIGIT-CLASS</code>,
299 <code>:NON-DIGIT-CLASS</code>, <code>:WORD-CHAR-CLASS</code>,
300 <code>:NON-WORD-CHAR-CLASS</code>,
301 <code>:WHITESPACE-CHAR-CLASS</code>, and
302 <code>:NON-WHITESPACE-CHAR-CLASS</code> are equivalent to Perl's
303 <em>special character classes</em> <code>&quot;\d&quot;</code>,
304 <code>&quot;\D&quot;</code>, <code>&quot;\w&quot;</code>,
305 <code>&quot;\W&quot;</code>, <code>&quot;\s&quot;</code>, and
306 <code>&quot;\S&quot;</code> respectively.
307
308 <li>The symbols <code>:START-ANCHOR</code>, <code>:END-ANCHOR</code>,
309 <code>:MODELESS-START-ANCHOR</code>,
310 <code>:MODELESS-END-ANCHOR</code>, and
311 <code>:MODELESS-END-ANCHOR-NO-NEWLINE</code> are equivalent to Perl's
312 <code>&quot;^&quot;</code>, <code>&quot;$&quot;</code>,
313 <code>&quot;\A&quot;</code>, <code>&quot;\Z&quot;</code>, and
314 <code>&quot;\z&quot;</code> respectively.
315
316 <li>The symbols <code>:CASE-INSENSITIVE-P</code>,
317 <code>:CASE-SENSITIVE-P</code>, <code>:MULTI-LINE-MODE-P</code>,
318 <code>:NOT-MULTI-LINE-MODE-P</code>, <code>:SINGLE-LINE-MODE-P</code>,
319 and <code>:NOT-SINGLE-LINE-MODE-P</code> are equivalent to Perl's
320 <em>embedded modifiers</em> <code>&quot;(?i)&quot;</code>,
321 <code>&quot;(?-i)&quot;</code>, <code>&quot;(?m)&quot;</code>,
322 <code>&quot;(?-m)&quot;</code>, <code>&quot;(?s)&quot;</code>, and
323 <code>&quot;(?-s)&quot;</code>. As usual, changes applied to modes are
324 kept local to the innermost enclosing grouping or clustering
325 construct.
326
327 </li><li>All other symbols will signal an error of type <a
328 href="#ppcre-syntax-error"><code>PPCRE-SYNTAX-ERROR</code></a>
329 <em>unless</em> they are defined to be <a
330 href="#parse-tree-synonym"><em>parse tree synonyms</em></a>.
331
332 <li><code>(:FLAGS {&lt;modifier&gt;}*)</code> where
333 <code>&lt;modifier&gt;</code> is one of the modifier symbols from
334 above is used to group modifier symbols. The modifiers are applied
335 from left to right. (This construct is obviously redundant. It is only
336 there because it's used by the parser.)
337
338 <li><code>(:SEQUENCE {&lt;<i>parse-tree</i>&gt;}*)</code> means a
339 sequence of parse trees, i.e. the parse trees must match one after
340 another. Example: <code>(:SEQUENCE #\f #\o #\o)</code> is equivalent
341 to the parse tree <code>&quot;foo&quot;</code>.
342
343 <li><code>(:GROUP {&lt;<i>parse-tree</i>&gt;}*)</code> is like
344 <code>:SEQUENCE</code> but changes applied to modifier flags (see
345 above) are kept local to the parse trees enclosed by this
346 construct. Think of it as the S-expression variant of Perl's
347 <code>&quot;(?:&lt;<i>pattern</i>&gt;)&quot;</code> construct.
348
349 <li><code>(:ALTERNATION {&lt;<i>parse-tree</i>&gt;}*)</code> means an
350 alternation of parse trees, i.e. one of the parse trees must
351 match. Example: <code>(:ALTERNATION #\b #\a #\z)</code> is equivalent
352 to the Perl regex string <code>&quot;b|a|z&quot;</code>.
353
354 <li><code>(:BRANCH &lt;<i>test</i>&gt;
355 &lt;<i>parse-tree</i>&gt;)</code> is for conditional regular
356 expressions. <code>&lt;<i>test</i>&gt;</code> is either a number which
357 stands for a register or a parse tree which is a look-ahead or
358 look-behind assertion. See the entry for
359 <code>(?(&lt;<i>condition</i>&gt;)&lt;<i>yes-pattern</i>&gt;|&lt;<i>no-pattern</i>&gt;)</code>
360 in <a
361 href="http://perldoc.perl.org/perlre.html#Extended-Patterns"><code>man
362 perlre</code></a> for the semantics of this construct. If
363 <code>&lt;<i>parse-tree</i>&gt;</code> is an alternation is
364 <em>must</em> enclose exactly one or two parse trees where the second
365 one (if present) will be treated as the &quot;no-pattern&quot; - in
366 all other cases <code>&lt;<i>parse-tree</i>&gt;</code> will be treated
367 as the &quot;yes-pattern&quot;.
368
369 <li><code>(:POSITIVE-LOOKAHEAD|:NEGATIVE-LOOKAHEAD|:POSITIVE-LOOKBEHIND|:NEGATIVE-LOOKBEHIND
370 &lt;<i>parse-tree</i>&gt;)</code> should be pretty obvious...
371
372 <li><code>(:GREEDY-REPETITION|:NON-GREEDY-REPETITION
373 &lt;<i>min</i>&gt; &lt;<i>max</i>&gt;
374 &lt;<i>parse-tree</i>&gt;)</code> where
375 <code>&lt;<i>min</i>&gt;</code> is a non-negative integer and
376 <code>&lt;<i>max</i>&gt;</code> is either a non-negative integer not
377 smaller than <code>&lt;<i>min</i>&gt;</code> or <code>NIL</code> will
378 result in a regular expression which tries to match
379 <code>&lt;<i>parse-tree</i>&gt;</code> at least
380 <code>&lt;<i>min</i>&gt;</code> times and at most
381 <code>&lt;<i>max</i>&gt;</code> times (or as often as possible if
382 <code>&lt;<i>max</i>&gt;</code> is <code>NIL</code>). So, e.g.,
383 <code>(:NON-GREEDY-REPETITION 0 1 &quot;ab&quot;)</code> is equivalent
384 to the Perl regex string <code>&quot;(?:ab)??&quot;</code>.
385
386 <li><code>(:STANDALONE &lt;<i>parse-tree</i>&gt;)</code> is an
387 &quot;independent&quot; subexpression, i.e. <code>(:STANDALONE
388 &quot;bar&quot;)</code> is equivalent to the Perl regex string
389 <code>&quot;(?>bar)&quot;</code>.
390
391 <li><code>(:REGISTER &lt;<i>parse-tree</i>&gt;)</code> is a capturing
392 register group. As usual, registers are counted from left to right
393 beginning with 1.
394
395 <li><code>(:NAMED-REGISTER &lt;<i>name</i>&gt; &lt;<i>parse-tree</i>&gt;)</code> is a named capturing
396 register group. Acts as <code>:REGISTER</code>, but assigns <code>&lt;<i>name</i>&gt;</code> to a register too. This <code>&lt;<i>name</i>&gt;</code> can be later referred to via <code>:BACK-REFERENCE</code>. Names are case-sensitive and don't need to be unique. See <a href="#*allow-named-registers*"><code>*ALLOW-NAMED-REGISTERS*</code></a> for details.
397
398
399 <li><code>(:BACK-REFERENCE &lt;<i>ref</i>&gt;)</code> is a
400 back-reference to a register group. <code>&lt;<i>ref</i>&gt;</code> is
401 a positive integer or a string denoting a register name. If there are
402 several registers with the same name, the regex engine tries to
403 successfully match at least of them, starting with the most recently
404 seen register continuing to the least recently seen one, until a match
405 is found. See <a
406 href="#*allow-named-registers*"><code>*ALLOW-NAMED-REGISTERS*</code></a>
407 for more information.
408
409 <li><code>(:PROPERTY|:INVERTED-PROPERTY &lt;<i>property</i>&gt;)</code> is
410 a <a href="#*property-resolver*">named property</a> (or its inverse) with
411 <code>&lt;<i>property</i>&gt;</code> being a function designator or a
412 string which must be resolved
413 by <a href="#*property-resolver*"><code>*PROPERTY-RESOLVER*</code></a>.
414
415 <li><a class=none name="filterdef"><code>(:FILTER &lt;<i>function</i>&gt; <tt>&amp;optional</tt>
416 &lt;<i>length</i>&gt;)</code></a> where
417 <code>&lt;<i>function</i>&gt;</code> is a <a
418 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#function_designator">function
419 designator</a> and <code>&lt;<i>length</i>&gt;</code> is a
420 non-negative integer or <code>NIL</code> is a user-defined <a
421 href="#filters">filter</a>.
422
423 <li><code>(:REGEX &lt;<i>string</i>&gt;)</code> where
424 <code>&lt;<i>string</i>&gt;</code> is an
425 embedded <a href="#create-scanner">regular expression in Perl
426 syntax</a>.
427
428 <li><code>(:CHAR-CLASS|:INVERTED-CHAR-CLASS
429 {&lt;<i>item</i>&gt;}*)</code> where <code>&lt;<i>item</i>&gt;</code>
430 is either a character, a <em>character range</em>, a named property
431 (see above), or a symbol for a special character class (see above)
432 will be translated into a (one character wide) character
433 class. A <em>character range</em> looks like
434 <code>(:RANGE &lt;<i>char1</i>&gt; &lt;<i>char2</i>&gt;)</code> where
435 <code>&lt;<i>char1</i>&gt;</code> and
436 <code>&lt;<i>char2</i>&gt;</code> are characters such that
437 <code>(CHAR&lt;= &lt;<i>char1</i>&gt; &lt;<i>char2</i>&gt;)</code> is
438 true. Example: <code>(:INVERTED-CHAR-CLASS #\a (:RANGE #\D #\G)
439 :DIGIT-CLASS)</code> is equivalent to the Perl regex string
440 <code>&quot;[^aD-G\d]&quot;</code>.
441
442 </ul>
443
444 Because <code>CREATE-SCANNER</code> is defined as a generic function
445 which dispatches on its first argument there's a certain ambiguity:
446 Although strings are valid parse trees they will be interpreted as
447 Perl regex strings when given to <code>CREATE-SCANNER</code>. To
448 circumvent this you can always use the equivalent parse tree <code>(:GROUP
449 &lt;<i>string</i>&gt;)</code> instead.
450 <p>
451 Note that <code>CREATE-SCANNER</code> doesn't always check
452 for the well-formedness of its first argument, i.e. you are expected
453 to provide <em>correct</em> parse trees.
454
455 <p>
456 The usage of the keyword argument <code>extended-mode</code> obviously
457 doesn't make sense if <code>CREATE-SCANNER</code> is applied to parse
458 trees and will signal an error.
459 <p>
460 If <code>destructive</code> is not <code>NIL</code> (the default is
461 <code>NIL</code>), the function is allowed to destructively modify
462 <code><i>parse-tree</i></code> while creating the scanner.
463 <p>
464 If you want to find out how parse trees are related to Perl regex
465 strings, you should play around with
466 <a href="#parse-string"><code>PARSE-STRING</code></a>:
467
468 <pre>
469 * (parse-string "(ab)*")
470 (:GREEDY-REPETITION 0 NIL (:REGISTER "ab"))
471
472 * (parse-string "(a(b))")
473 (:REGISTER (:SEQUENCE #\a (:REGISTER #\b)))
474
475 * (parse-string "(?:abc){3,5}")
476 (:GREEDY-REPETITION 3 5 (:GROUP "abc"))
477 <font color=orange>;; (:GREEDY-REPETITION 3 5 "abc") would also be OK</font>
478
479 * (parse-string "a(?i)b(?-i)c")
480 (:SEQUENCE #\a
481 (:SEQUENCE (:FLAGS :CASE-INSENSITIVE-P)
482 (:SEQUENCE #\b (:SEQUENCE (:FLAGS :CASE-SENSITIVE-P) #\c))))
483 <font color=orange>;; same as (:SEQUENCE #\a :CASE-INSENSITIVE-P #\b :CASE-SENSITIVE-P #\c)</font>
484
485 * (parse-string "(?=a)b")
486 (:SEQUENCE (:POSITIVE-LOOKAHEAD #\a) #\b)
487 </pre></blockquote>
488
489 <p><br>
490 <font color=green><b>For the rest of the dictionary, </b><code><i>regex</i></code><b> can
491 always be a string (which is interpreted as a Perl regular
492 expression), a parse tree, or a scanner created by
493 <a href="#create-scanner"><font color=green><code>CREATE-SCANNER</code></font></a>. The
494 </b><code><i>start</i></code><b> and </b><code><i>end</i></code><b>
495 keyword parameters are always used as in <a
496 href="#scan"><font color=green><code>SCAN</code></font></a>.</b></font>
497
498
499
500
501 <p><br>[Generic Function]
502 <br><a class=none name="scan"><b>scan</b> <i>regex target-string <tt>&amp;key</tt> start end</i> =&gt; <i>match-start, match-end, reg-starts, reg-ends</i></a>
503
504 <blockquote><br>
505 Searches the string <code><i>target-string</i></code>
506 from <code><i>start</i></code> (which defaults to 0) to
507 <code><i>end</i></code> (which default to the length of
508 <code><i>target-string</i></code>) and tries to match
509 <code><i>regex</i></code>. On success returns four values - the start
510 of the match, the end of the match, and two arrays denoting the
511 beginnings and ends of register matches. On failure returns
512 <code>NIL</code>. <code><i>target-string</i></code> will be coerced
513 to a simple string if it isn't one already. (There's another keyword
514 parameter <code><i>real-start-pos</i></code>. This one should
515 <em>never</em> be set from user code - it is only used internally.)
516 <p>
517 <code>SCAN</code> acts as if the part of
518 <code><i>target-string</i></code> between <code><i>start</i></code>
519 and <code><i>end</i></code> were a standalone string, i.e. look-aheads
520 and look-behinds can't look beyond these boundaries.
521 <pre>
522 * (scan "(a)*b" "xaaabd")
523 1
524 5
525 #(3)
526 #(4)
527
528 * (scan "(a)*b" "xaaabd" :start 1)
529 1
530 5
531 #(3)
532 #(4)
533
534 * (scan "(a)*b" "xaaabd" :start 2)
535 2
536 5
537 #(3)
538 #(4)
539
540 * (scan "(a)*b" "xaaabd" :end 4)
541 NIL
542
543 * (scan '(:greedy-repetition 0 nil #\b) "bbbc")
544 0
545 3
546 #()
547 #()
548
549 * (scan '(:greedy-repetition 4 6 #\b) "bbbc")
550 NIL
551
552 * (let ((s (create-scanner "(([a-c])+)x")))
553 (scan s "abcxy"))
554 0
555 4
556 #(0 2)
557 #(3 3)
558 </pre></blockquote>
559
560
561
562 <p><br>[Function]
563 <br><a class=none name="scan-to-strings"><b>scan-to-strings</b> <i>regex target-string <tt>&amp;key</tt> start end sharedp</i> =&gt; <i>match, regs</i></a>
564
565 <blockquote><br>
566 Like <a href="#scan"><code>SCAN</code></a> but returns substrings of
567 <code><i>target-string</i></code> instead of positions, i.e. this
568 function returns two values on success: the whole match as a string
569 plus an array of substrings (or <code>NIL</code>s) corresponding to
570 the matched registers. If <code><i>sharedp</i></code> is true, the substrings may share structure with
571 <code><i>target-string</i></code>.
572 <pre>
573 * (scan-to-strings "[^b]*b" "aaabd")
574 "aaab"
575 #()
576
577 * (scan-to-strings "([^b])*b" "aaabd")
578 "aaab"
579 #("a")
580
581 * (scan-to-strings "(([^b])*)b" "aaabd")
582 "aaab"
583 #("aaa" "a")
584 </pre></blockquote>
585
586
587 <p><br>[Macro]
588 <br><a class=none name="register-groups-bind"><b>register-groups-bind</b> <i>var-list (regex target-string <tt>&amp;key</tt> start end sharedp) declaration* statement*</i> =&gt; <i>result*</i></a>
589
590 <blockquote><br>
591 Evaluates <code><i>statement*</i></code> with the variables in <code><i>var-list</i></code> bound to the
592 corresponding register groups after <code><i>target-string</i></code> has been matched
593 against <code><i>regex</i></code>, i.e. each variable is either
594 bound to a string or to <code>NIL</code>.
595 As a shortcut, the elements of <code><i>var-list</i></code> can also be lists of the form <code>(FN&nbsp;VAR)</code> where <code>VAR</code> is the variable symbol
596 and <code>FN</code> is a <a
597 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#function_designator">function
598 designator</a> (which is evaluated) denoting a function which is to be applied to the string before the result is bound to <code>VAR</code>.
599 To make this even more convenient the form <code>(FN&nbsp;VAR1&nbsp;...VARn)</code> can be used as an abbreviation for
600 <code>(FN&nbsp;VAR1)&nbsp;...&nbsp;(FN&nbsp;VARn)</code>.
601 <p>
602 If there is no match, the <code><i>statement*</i></code> forms are <em>not</em>
603 executed. For each element of
604 <code><i>var-list</i></code> which is <code>NIL</code> there's no binding to the corresponding register
605 group. The number of variables in <code><i>var-list</i></code> must not be greater than
606 the number of register groups. If <code><i>sharedp</i></code> is true, the substrings may
607 share structure with <code><i>target-string</i></code>.
608 <pre>
609 * (register-groups-bind (first second third fourth)
610 (&quot;((a)|(b)|(c))+&quot; &quot;abababc&quot; :sharedp t)
611 (list first second third fourth))
612 (&quot;c&quot; &quot;a&quot; &quot;b&quot; &quot;c&quot;)
613
614 * (register-groups-bind (nil second third fourth)
615 <font color=orange>;; note that we don't bind the first and fifth register group</font>
616 (&quot;((a)|(b)|(c))()+&quot; &quot;abababc&quot; :start 6)
617 (list second third fourth))
618 (NIL NIL &quot;c&quot;)
619
620 * (register-groups-bind (first)
621 (&quot;(a|b)+&quot; &quot;accc&quot; :start 1)
622 (format t &quot;This will not be printed: ~A&quot; first))
623 NIL
624
625 * (register-groups-bind (fname lname (#'parse-integer date month year))
626 (&quot;(\\w+)\\s+(\\w+)\\s+(\\d{1,2})\\.(\\d{1,2})\\.(\\d{4})&quot; &quot;Frank Zappa 21.12.1940&quot;)
627 (list fname lname (encode-universal-time 0 0 0 date month year 0)))
628 ("Frank" "Zappa" 1292889600)
629 </pre>
630 </blockquote>
631
632 <p><br>[Macro]
633 <br><a class=none name="do-scans"><b>do-scans</b> <i>(match-start match-end reg-starts reg-ends regex target-string <tt>&amp;optional</tt> result-form <tt>&amp;key</tt> start end) declaration* statement*</i> =&gt; <i>result*</i></a>
634
635 <blockquote><br>
636 A macro which iterates over <code><i>target-string</i></code> and
637 tries to match <code><i>regex</i></code> as often as possible
638 evaluating <code><i>statement*</i></code> with
639 <code><i>match-start</i></code>, <code><i>match-end</i></code>,
640 <code><i>reg-starts</i></code>, and <code><i>reg-ends</i></code> bound
641 to the four return values of each match (see <a
642 href="#scan"><code>SCAN</code></a>) in turn. After the last match,
643 returns <code><i>result-form</i></code> if provided or
644 <code>NIL</code> otherwise. An implicit block named <code>NIL</code>
645 surrounds <code>DO-SCANS</code>; <code>RETURN</code> may be used to
646 terminate the loop immediately. If <code><i>regex</i></code> matches
647 an empty string, the scan is continued one position behind this match.
648 <p>
649 This is the most general macro to iterate over all matches in a target
650 string. See the source code of <a
651 href="#do-matches"><code>DO-MATCHES</code></a>, <a
652 href="#all-matches"><code>ALL-MATCHES</code></a>, <a
653 href="#split"><code>SPLIT</code></a>, or <a
654 href="#regex-replace-all"><code>REGEX-REPLACE-ALL</code></a> for examples of its
655 usage.</blockquote>
656
657
658
659
660 <p><br>[Macro]
661 <br><a class=none name="do-matches"><b>do-matches</b> <i>(match-start match-end regex target-string <tt>&amp;optional</tt> result-form <tt>&amp;key</tt> start end) declaration* statement*</i> =&gt; <i>result*</i></a>
662
663 <blockquote><br>
664 Like <a href="#do-scans"><code>DO-SCANS</code></a> but doesn't bind
665 variables to the register arrays.
666 <pre>
667 * (defun foo (regex target-string &amp;key (start 0) (end (length target-string)))
668 (let ((sum 0))
669 (do-matches (s e regex target-string nil :start start :end end)
670 (incf sum (- e s)))
671 (format t "~,2F% of the string was inside of a match~%"
672 <font color=orange>;; note: doesn't check for division by zero</font>
673 (float (* 100 (/ sum (- end start)))))))
674
675 FOO
676
677 * (foo "a" "abcabcabc")
678 33.33% of the string was inside of a match
679 NIL
680 * (foo "aa|b" "aacabcbbc")
681 55.56% of the string was inside of a match
682 NIL
683 </pre></blockquote>
684
685
686
687
688 <p><br>[Macro]
689 <br><a class=none name="do-matches-as-strings"><b>do-matches-as-strings</b> <i>(match-var regex target-string <tt>&amp;optional</tt> result-form <tt>&amp;key</tt> start end sharedp) declaration* statement*</i> =&gt; <i>result*</i></a>
690
691 <blockquote><br>
692 Like <a href="#do-matches"><code>DO-MATCHES</code></a> but binds
693 <code><i>match-var</i></code> to the substring of
694 <code><i>target-string</i></code> corresponding to each match in turn. If <code><i>sharedp</i></code> is true, the substrings may share structure with
695 <code><i>target-string</i></code>.
696 <pre>
697 * (defun crossfoot (target-string &amp;key (start 0) (end (length target-string)))
698 (let ((sum 0))
699 (do-matches-as-strings (m :digit-class
700 target-string nil
701 :start start :end end)
702 (incf sum (parse-integer m)))
703 (if (< sum 10)
704 sum
705 (crossfoot (format nil "~A" sum)))))
706
707 CROSSFOOT
708
709 * (crossfoot "bar")
710 0
711
712 * (crossfoot "a3x")
713 3
714
715 * (crossfoot "12345")
716 6
717 </pre>
718
719 Of course, in real life you would do this with <a href="#do-matches"><code>DO-MATCHES</code></a> and use the <code><i>start</i></code> and <code><i>end</i></code> keyword parameters of <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_parse_.htm"><code>PARSE-INTEGER</code></a>.</blockquote>
720
721 <p><br>[Macro]
722 <br><a class=none name="do-register-groups"><b>do-register-groups</b> <i>var-list (regex target-string <tt>&amp;optional</tt> result-form <tt>&amp;key</tt> start end sharedp) declaration* statement*</i> =&gt; <i>result*</i></a>
723
724 <blockquote><br>
725 Iterates over <code><i>target-string</i></code> and tries to match <code><i>regex</i></code> as often as
726 possible evaluating <code><i>statement*</i></code> with the variables in <code><i>var-list</i></code> bound to the
727 corresponding register groups for each match in turn, i.e. each
728 variable is either bound to a string or to <code>NIL</code>. You can use the same shortcuts and abbreviations as in <a href="#register-groups-bind"><code>REGISTER-GROUPS-BIND</code></a>. The number of
729 variables in <code><i>var-list</i></code> must not be greater than the number of register
730 groups. For each element of
731 <code><i>var-list</i></code> which is <code>NIL</code> there's no binding to the corresponding register
732 group. After the last match, returns <code><i>result-form</i></code> if provided or <code>NIL</code>
733 otherwise. An implicit block named <code>NIL</code> surrounds <code>DO-REGISTER-GROUPS</code>;
734 <code>RETURN</code> may be used to terminate the loop immediately. If <code><i>regex</i></code> matches
735 an empty string, the scan is continued one position behind this
736 match. If <code><i>sharedp</i></code> is true, the substrings may share structure with
737 <code><i>target-string</i></code>.
738 <pre>
739 * (do-register-groups (first second third fourth)
740 (&quot;((a)|(b)|(c))&quot; &quot;abababc&quot; nil :start 2 :sharedp t)
741 (print (list first second third fourth)))
742 (&quot;a&quot; &quot;a&quot; NIL NIL)
743 (&quot;b&quot; NIL &quot;b&quot; NIL)
744 (&quot;a&quot; &quot;a&quot; NIL NIL)
745 (&quot;b&quot; NIL &quot;b&quot; NIL)
746 (&quot;c&quot; NIL NIL &quot;c&quot;)
747 NIL
748
749 * (let (result)
750 (do-register-groups ((#'parse-integer n) (#'intern sign) whitespace)
751 (&quot;(\\d+)|(\\+|-|\\*|/)|(\\s+)&quot; &quot;12*15 - 42/3&quot;)
752 (unless whitespace
753 (push (or n sign) result)))
754 (nreverse result))
755 (12 * 15 - 42 / 3)
756 </pre>
757 </blockquote>
758
759
760 <p><br>[Function]
761 <br><a class=none name="all-matches"><b>all-matches</b> <i>regex target-string <tt>&amp;key</tt> start end</i> =&gt; <i>list</i></a>
762
763 <blockquote><br>
764 Returns a list containing the start and end positions of all matches
765 of <code><i>regex</i></code> against
766 <code><i>target-string</i></code>, i.e. if there are <code>N</code>
767 matches the list contains <code>(* 2 N)</code> elements. If
768 <code><i>regex</i></code> matches an empty string the scan is
769 continued one position behind this match.
770 <pre>
771 * (all-matches "a" "foo bar baz")
772 (5 6 9 10)
773
774 * (all-matches "\\w*" "foo bar baz")
775 (0 3 3 3 4 7 7 7 8 11 11 11)
776 </pre></blockquote>
777
778
779
780
781 <p><br>[Function]
782 <br><a class=none name="all-matches-as-strings"><b>all-matches-as-strings</b> <i>regex target-string <tt>&amp;key</tt> start end sharedp</i> =&gt; <i>list</i></a>
783
784 <blockquote><br>
785 Like <a href="#all-matches"><code>ALL-MATCHES</code></a> but
786 returns a list of substrings instead. If <code><i>sharedp</i></code> is true, the substrings may share structure with
787 <code><i>target-string</i></code>.
788 <pre>
789 * (all-matches-as-strings "a" "foo bar baz")
790 ("a" "a")
791
792 * (all-matches-as-strings "\\w*" "foo bar baz")
793 ("foo" "" "bar" "" "baz" "")
794 </pre></blockquote>
795
796
797
798
799 <h4><a name="splitting" class=none>Splitting and replacing</a></h4>
800
801
802 <p><br>[Function]
803 <br><a class=none name="split"><b>split</b> <i>regex target-string <tt>&amp;key</tt> start end limit with-registers-p omit-unmatched-p sharedp</i> =&gt; <i>list</i></a>
804
805 <blockquote><br>
806 Matches <code><i>regex</i></code> against
807 <code><i>target-string</i></code> as often as possible and returns a
808 list of the substrings between the matches. If
809 <code><i>with-registers-p</i></code> is true, substrings corresponding
810 to matched registers are inserted into the list as well. If
811 <code><i>omit-unmatched-p</i></code> is true, unmatched registers will
812 simply be left out, otherwise they will show up as
813 <code>NIL</code>. <code><i>limit</i></code> limits the number of
814 elements returned - registers aren't counted. If
815 <code><i>limit</i></code> is <code>NIL</code> (or 0 which is
816 equivalent), trailing empty strings are removed from the result list.
817 If <code><i>regex</i></code> matches an empty string, the scan is
818 continued one position behind this match. If <code><i>sharedp</i></code> is true, the substrings may share structure with
819 <code><i>target-string</i></code>.
820 <p>
821 This function also tries hard to be
822 Perl-compatible - thus the somewhat peculiar behaviour.
823 <pre>
824 * (split "\\s+" "foo bar baz
825 frob")
826 ("foo" "bar" "baz" "frob")
827
828 * (split "\\s*" "foo bar baz")
829 ("f" "o" "o" "b" "a" "r" "b" "a" "z")
830
831 * (split "(\\s+)" "foo bar baz")
832 ("foo" "bar" "baz")
833
834 * (split "(\\s+)" "foo bar baz" :with-registers-p t)
835 ("foo" " " "bar" " " "baz")
836
837 * (split "(\\s)(\\s*)" "foo bar baz" :with-registers-p t)
838 ("foo" " " "" "bar" " " " " "baz")
839
840 * (split "(,)|(;)" "foo,bar;baz" :with-registers-p t)
841 ("foo" "," NIL "bar" NIL ";" "baz")
842
843 * (split "(,)|(;)" "foo,bar;baz" :with-registers-p t :omit-unmatched-p t)
844 ("foo" "," "bar" ";" "baz")
845
846 * (split ":" "a:b:c:d:e:f:g::")
847 ("a" "b" "c" "d" "e" "f" "g")
848
849 * (split ":" "a:b:c:d:e:f:g::" :limit 1)
850 ("a:b:c:d:e:f:g::")
851
852 * (split ":" "a:b:c:d:e:f:g::" :limit 2)
853 ("a" "b:c:d:e:f:g::")
854
855 * (split ":" "a:b:c:d:e:f:g::" :limit 3)
856 ("a" "b" "c:d:e:f:g::")
857
858 * (split ":" "a:b:c:d:e:f:g::" :limit 1000)
859 ("a" "b" "c" "d" "e" "f" "g" "" "")
860 </pre></blockquote>
861
862
863
864
865
866 <p><br>[Function]
867 <br><a class=none name="regex-replace"><b>regex-replace</b> <i>regex target-string replacement <tt>&amp;key</tt> start end preserve-case simple-calls element-type</i> =&gt; <i>string, matchp</i></a>
868
869 <blockquote><br> Try to match <code><i>target-string</i></code>
870 between <code><i>start</i></code> and <code><i>end</i></code> against
871 <code><i>regex</i></code> and replace the first match with
872 <code><i>replacement</i></code>. Two values are returned; the modified
873 string, and <code>T</code> if <code><i>regex</i></code> matched or
874 <code>NIL</code> otherwise.
875 <p>
876 <code><i>replacement</i></code> can be a string which may contain the
877 special substrings <code>&quot;\&amp;&quot;</code> for the whole
878 match, <code>&quot;\`&quot;</code> for the part of
879 <code><i>target-string</i></code> before the match,
880 <code>&quot;\'&quot;</code> for the part of
881 <code><i>target-string</i></code> after the match,
882 <code>&quot;\N&quot;</code> or <code>&quot;\{N}&quot;</code> for the
883 <code>N</code>th register where <code>N</code> is a positive integer.
884 <p>
885 <code><i>replacement</i></code> can also be a <a
886 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#function_designator">function
887 designator</a> in which case the match will be replaced with the
888 result of calling the function designated by
889 <code><i>replacement</i></code> with the arguments
890 <code><i>target-string</i></code>, <code><i>start</i></code>,
891 <code><i>end</i></code>, <code><i>match-start</i></code>,
892 <code><i>match-end</i></code>, <code><i>reg-starts</i></code>, and
893 <code><i>reg-ends</i></code>. (<code><i>reg-starts</i></code> and
894 <code><i>reg-ends</i></code> are arrays holding the start and end
895 positions of matched registers (or <code>NIL</code>) - the meaning of
896 the other arguments should be obvious.)
897 <p>
898 If <code><i>simple-calls</i></code> is true, a function designated by
899 <code><i>replacement</i></code> will instead be called with the
900 arguments <code><i>match</i></code>, <code><i>register-1</i></code>,
901 ..., <code><i>register-n</i></code> where <code><i>match</i></code> is
902 the whole match as a string and <code><i>register-1</i></code> to
903 <code><i>register-n</i></code> are the matched registers, also as
904 strings (or <code>NIL</code>). Note that these strings share structure with
905 <code><i>target-string</i></code> so you must not modify them.
906 <p>
907 Finally, <code><i>replacement</i></code> can be a list where each
908 element is a string (which will be inserted verbatim), one of the
909 symbols <code>:match</code>, <code>:before-match</code>, or
910 <code>:after-match</code> (corresponding to
911 <code>&quot;\&amp;&quot;</code>, <code>&quot;\`&quot;</code>, and
912 <code>&quot;\'&quot;</code> above), an integer <code>N</code>
913 (representing register <code>(1+&nbsp;N)</code>), or a function
914 designator.
915 <p>
916 If <code><i>preserve-case</i></code> is true (default is
917 <code>NIL</code>), the replacement will try to preserve the case (all
918 upper case, all lower case, or capitalized) of the match. The result
919 will always be a <a
920 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#fresh">fresh</a>
921 string, even if <code><i>regex</i></code> doesn't match.
922 <p>
923 <code><i>element-type</i></code> specifies
924 the <a
925 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_a.htm#array_element_type">array
926 element type</a> of the string which is returned, the default
927 is <a
928 href="http://www.lispworks.com/documentation/lw50/LWRM/html/lwref-346.htm"><code>LW:SIMPLE-CHAR</code></a>
929 for LispWorks
930 and <a
931 href="http://www.lispworks.com/documentation/HyperSpec/Body/t_ch.htm"><code>CHARACTER</code></a>
932 for other Lisps.
933 <pre>
934 * (regex-replace "fo+" "foo bar" "frob")
935 "frob bar"
936 T
937
938 * (regex-replace "fo+" "FOO bar" "frob")
939 "FOO bar"
940 NIL
941
942 * (regex-replace "(?i)fo+" "FOO bar" "frob")
943 "frob bar"
944 T
945
946 * (regex-replace "(?i)fo+" "FOO bar" "frob" :preserve-case t)
947 "FROB bar"
948 T
949
950 * (regex-replace "(?i)fo+" "Foo bar" "frob" :preserve-case t)
951 "Frob bar"
952 T
953
954 * (regex-replace "bar" "foo bar baz" "[frob (was '\\&' between '\\`' and '\\'')]")
955 "foo [frob (was 'bar' between 'foo ' and ' baz')] baz"
956 T
957
958 * (regex-replace "bar" "foo bar baz"
959 '("[frob (was '" :match "' between '" :before-match "' and '" :after-match "')]"))
960 "foo [frob (was 'bar' between 'foo ' and ' baz')] baz"
961 T
962
963 * (regex-replace "(be)(nev)(o)(lent)"
964 "benevolent: adj. generous, kind"
965 #'(lambda (match &amp;rest registers)
966 (format nil "~A [~{~A~^.~}]" match registers))
967 :simple-calls t)
968 "benevolent [be.nev.o.lent]: adj. generous, kind"
969 T
970 </pre></blockquote>
971
972
973 <p><br>[Function]
974 <br><a class=none name="regex-replace-all"><b>regex-replace-all</b> <i>regex target-string replacement <tt>&amp;key</tt> start end preserve-case simple-calls element-type</i> =&gt; <i>string, matchp</i></a>
975
976 <blockquote><br>
977 Like <a href="#regex-replace"><code>REGEX-REPLACE</code></a> but replaces all matches.
978 <pre>
979 * (regex-replace-all "(?i)fo+" "foo Fooo FOOOO bar" "frob" :preserve-case t)
980 "frob Frob FROB bar"
981 T
982
983 * (regex-replace-all "(?i)f(o+)" "foo Fooo FOOOO bar" "fr\\1b" :preserve-case t)
984 "froob Frooob FROOOOB bar"
985 T
986
987 * (let ((qp-regex (create-scanner "[\\x80-\\xff]")))
988 (defun encode-quoted-printable (string)
989 "Converts 8-bit string to quoted-printable representation."
990 <font color=orange>;; won't work for Corman Lisp because non-ASCII characters aren't 8-bit there</font>
991 (flet ((convert (target-string start end match-start match-end reg-starts reg-ends)
992 (declare (ignore start end match-end reg-starts reg-ends))
993 (format nil "=~2,'0x" (char-code (char target-string match-start)))))
994 (regex-replace-all qp-regex string #'convert))))
995 Converted ENCODE-QUOTED-PRINTABLE.
996 ENCODE-QUOTED-PRINTABLE
997
998 * (encode-quoted-printable "F&ecirc;te S&oslash;rensen na&iuml;ve H&uuml;hner Stra&szlig;e")
999 "F=EAte S=F8rensen na=EFve H=FChner Stra=DFe"
1000 T
1001
1002 * (let ((url-regex (create-scanner "[^a-zA-Z0-9_\\-.]")))
1003 (defun url-encode (string)
1004 "URL-encodes a string."
1005 <font color=orange>;; won't work for Corman Lisp because non-ASCII characters aren't 8-bit there</font>
1006 (flet ((convert (target-string start end match-start match-end reg-starts reg-ends)
1007 (declare (ignore start end match-end reg-starts reg-ends))
1008 (format nil "%~2,'0x" (char-code (char target-string match-start)))))
1009 (regex-replace-all url-regex string #'convert))))
1010 Converted URL-ENCODE.
1011 URL-ENCODE
1012
1013 * (url-encode "F&ecirc;te S&oslash;rensen na&iuml;ve H&uuml;hner Stra&szlig;e")
1014 "F%EAte%20S%F8rensen%20na%EFve%20H%FChner%20Stra%DFe"
1015 T
1016
1017 * (defun how-many (target-string start end match-start match-end reg-starts reg-ends)
1018 (declare (ignore start end match-start match-end))
1019 (format nil "~A" (- (svref reg-ends 0)
1020 (svref reg-starts 0))))
1021 HOW-MANY
1022
1023 * (regex-replace-all "{(.+?)}"
1024 "foo{...}bar{.....}{..}baz{....}frob"
1025 (list "[" 'how-many " dots]"))
1026 "foo[3 dots]bar[5 dots][2 dots]baz[4 dots]frob"
1027 T
1028
1029 * (let ((qp-regex (create-scanner "[\\x80-\\xff]")))
1030 (defun encode-quoted-printable (string)
1031 "Converts 8-bit string to quoted-printable representation.
1032 Version using SIMPLE-CALLS keyword argument."
1033 <font color=orange>;; ;; won't work for Corman Lisp because non-ASCII characters aren't 8-bit there</font>
1034 (flet ((convert (match)
1035 (format nil "=~2,'0x" (char-code (char match 0)))))
1036 (regex-replace-all qp-regex string #'convert
1037 :simple-calls t))))
1038
1039 Converted ENCODE-QUOTED-PRINTABLE.
1040 ENCODE-QUOTED-PRINTABLE
1041
1042 * (encode-quoted-printable "F&ecirc;te S&oslash;rensen na&iuml;ve H&uuml;hner Stra&szlig;e")
1043 "F=EAte S=F8rensen na=EFve H=FChner Stra=DFe"
1044 T
1045
1046 * (defun how-many (match first-register)
1047 (declare (ignore match))
1048 (format nil "~A" (length first-register)))
1049 HOW-MANY
1050
1051 * (regex-replace-all "{(.+?)}"
1052 "foo{...}bar{.....}{..}baz{....}frob"
1053 (list "[" 'how-many " dots]")
1054 :simple-calls t)
1055
1056 "foo[3 dots]bar[5 dots][2 dots]baz[4 dots]frob"
1057 T
1058 </pre></blockquote>
1059
1060 <h4><a name="modify" class=none>Modifying scanner behaviour</a></h4>
1061
1062 <p><br>[Special variable]
1063 <br><a class=none name="*property-resolver*"><b>*property-resolver*</b></a>
1064
1065 </p><blockquote><br> This is the designator for a function responsible
1066 for resolving named properties like <code>\p{Number}</code>. If
1067 CL-PPCRE encounters a <code>\p</code> or a <code>\P</code> it expects
1068 to see an opening curly brace immediately afterwards and will then
1069 read everything following that brace until it sees a closing curly
1070 brace. The resolver function will be called with this string and must
1071 return a corresponding unary test function which accepts a character
1072 as its argument and returns a true value if and only if the character
1073 has the named property. If the resolver returns <code>NIL</code>
1074 instead, it signals that a property of that name is unknown.
1075 <pre>
1076 * (labels ((char-code-odd-p (char)
1077 (oddp (char-code char)))
1078 (char-code-even-p (char)
1079 (evenp (char-code char)))
1080 (resolver (name)
1081 (cond ((string= name "odd") #'char-code-odd-p)
1082 ((string= name "even") #'char-code-even-p)
1083 ((string= name "true") (constantly t))
1084 (t (error "Can't resolve ~S." name)))))
1085 (let ((*property-resolver* #'resolver))
1086 <font color=orange>;; quiz question - why do we need CREATE-SCANNER here?</font>
1087 (list (regex-replace-all (create-scanner "\\p{odd}") "abcd" "+")
1088 (regex-replace-all (create-scanner "\\p{even}") "abcd" "+")
1089 (regex-replace-all (create-scanner "\\p{true}") "abcd" "+"))))
1090 ("+b+d" "a+c+" "++++")
1091 </pre>
1092 If the value
1093 of <a href="#*property-resolver*"><code>*PROPERTY-RESOLVER*</code></a>
1094 is <code>NIL</code> (which is the default), <code>\p</code> and <code>\P</code> in regex
1095 strings will simply be treated like <code>p</code> or <code>P</code>
1096 as in CL-PPCRE&nbsp;1.4.1 and earlier. Note that this does not affect
1097 the validity of <code>(:PROPERTY&nbsp;&lt;<i>name</i>&gt;)</code>
1098 parts in <a href="#create-scanner2">S-expression syntax</a>.
1099 </blockquote>
1100
1101
1102 <p><br>[Accessor]
1103 <br><a class="none" name="parse-tree-synonym"><b>parse-tree-synonym</b> <i>symbol</i> =&gt; <i>parse-tree</i>
1104 <br><tt>(setf (</tt><b>parse-tree-synonym</b> <i>symbol</i><tt>)</tt> <i>new-parse-tree</i><tt>)</tt></a>
1105
1106 </p><blockquote><br>
1107 Any symbol (unless it's a keyword with a special meaning in parse
1108 trees) can be made a "synonym", i.e. an abbreviation, for another parse
1109 tree by this accessor. <code>PARSE-TREE-SYNONYM</code> returns <code>NIL</code> if <code><i>symbol</i></code> isn't a synonym yet.
1110 <pre>
1111 * (parse-string "a*b+")
1112 (:SEQUENCE (:GREEDY-REPETITION 0 NIL #\a) (:GREEDY-REPETITION 1 NIL #\b))
1113
1114 * (defun my-repetition (char min)
1115 `(:greedy-repetition ,min nil ,char))
1116 MY-REPETITION
1117
1118 * (setf (parse-tree-synonym 'a*) (my-repetition #\a 0))
1119 (:GREEDY-REPETITION 0 NIL #\a)
1120
1121 * (setf (parse-tree-synonym 'b+) (my-repetition #\b 1))
1122 (:GREEDY-REPETITION 1 NIL #\b)
1123
1124 * (let ((scanner (create-scanner '(:sequence a* b+))))
1125 (dolist (string '("ab" "b" "aab" "a" "x"))
1126 (print (scan scanner string)))
1127 (values))
1128 0
1129 0
1130 0
1131 NIL
1132 NIL
1133
1134 * (parse-tree-synonym 'a*)
1135 (:GREEDY-REPETITION 0 NIL #\a)
1136
1137 * (parse-tree-synonym 'a+)
1138 NIL
1139 </pre></blockquote>
1140
1141 <p><br>[Macro]
1142 <br><a class="none" name="define-parse-tree-synonym"><b>define-parse-tree-synonym</b> <i>name parse-tree</i> =&gt; <i>parse-tree</i></a>
1143
1144 </p><blockquote><br>
1145 This is a convenience macro for parse tree synonyms defined as
1146
1147 <pre>
1148 (defmacro define-parse-tree-synonym (name parse-tree)
1149 `(eval-when (:compile-toplevel :load-toplevel :execute)
1150 (setf (parse-tree-synonym ',name) ',parse-tree)))
1151 </pre>
1152
1153 so you can write code like this:
1154
1155 <pre>
1156 (define-parse-tree-synonym a-z
1157 (:char-class (:range #\a #\z) (:range #\A #\Z)))
1158
1159 (define-parse-tree-synonym a-z*
1160 (:greedy-repetition 0 nil a-z))
1161
1162 (defun ascii-char-tester (string)
1163 (scan '(:sequence :start-anchor a-z* :end-anchor)
1164 string))
1165 </pre></blockquote>
1166
1167 <p><br>[Special variable]
1168 <br><a class=none name="*regex-char-code-limit*"><b>*regex-char-code-limit*</b></a>
1169
1170 <blockquote><br>This variable controls whether scanners take into
1171 account all characters of your CL implementation or only those
1172 the <a
1173 href="http://www.lispworks.com/documentation/HyperSpec/Body/f_char_c.htm#char-code"><code>CHAR-CODE</code></a>
1174 of which is not larger than its value. The default is
1175 <a href="http://www.lispworks.com/documentation/HyperSpec/Body/v_char_c.htm"><code>CHAR-CODE-LIMIT</code></a>,
1176 and you might see significant speed and space improvements during
1177 scanner <em>creation</em> if, say, your target strings only
1178 contain <a href="http://czyborra.com/charsets/iso8859.html">ISO-8859-1</a>
1179 characters and you're using a Lisp implementation
1180 where <code>CHAR-CODE-LIMIT</code> has a value much higher
1181 than&nbsp;256. The <a href="#test">test suite</a> will automatically
1182 set <code>*REGEX-CHAR-CODE-LIMIT*</code> to 256 while you're running
1183 the default test.
1184 <p>
1185 Note: Due to the nature of <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_ld_tim.htm"><code>LOAD-TIME-VALUE</code></a> and the <a
1186 href="#compiler-macro">compiler macro for <code>SCAN</code> and other functions</a>, some
1187 scanners might be created in a <a
1188 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_n.htm#null_lexical_environment">null
1189 lexical environment</a> at load time or at compile time so be careful
1190 to which value <code>*REGEX-CHAR-CODE-LIMIT*</code> is bound at that
1191 time. The default value should always yield correct results unless you
1192 play dirty tricks with implementation-dependent behaviour, though.</blockquote>
1193
1194 <p><br>[Special variable]
1195 <br><a class=none name="*use-bmh-matchers*"><b>*use-bmh-matchers*</b></a>
1196
1197 <blockquote><br>Usually, the scanners created
1198 by <a href="#create-scanner"><code>CREATE-SCANNER</code></a> (or
1199 implicitly by other functions and macros) will use the standard
1200 function <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_search.htm"><code>SEARCH</code></a>
1201 to check for constant strings at the start or end of the regular
1202 expression. If <code>*USE-BMH-MATCHERS*</code> is true (the default
1203 is <code>NIL</code>),
1204 fast <a href="http://www-igm.univ-mlv.fr/~lecroq/string/node18.html">Boyer-Moore-Horspool
1205 matchers</a> will be used instead. This will usually be faster but
1206 can make the scanners considerably bigger. Per BMH matcher - there
1207 can be up to two per scanner - a fixnum array of
1208 size <a href="#*regex-char-code-limit*"><code>*REGEX-CHAR-CODE-LIMIT*</code></a>
1209 is allocated and closed over.
1210 <p>
1211 Note: Due to the nature of <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_ld_tim.htm"><code>LOAD-TIME-VALUE</code></a> and the <a
1212 href="#compiler-macro">compiler macro for <code>SCAN</code> and other functions</a>, some
1213 scanners might be created in a <a
1214 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_n.htm#null_lexical_environment">null
1215 lexical environment</a> at load time or at compile time so be careful
1216 to which value <code>*USE-BMH-MATCHERS*</code> is bound at that
1217 time.</blockquote>
1218
1219 <p><br>[Special variable]<br><a class=none name='*optimize-char-classes*'><b>*optimize-char-classes*</b></a>
1220 <blockquote><br>
1221 Whether character classes should be compiled into look-ups into <em>O(1)</em>
1222 data structures. This is usually fast but will be costly in terms of
1223 scanner creation time and might be costly in terms of size if
1224 <a href="#*regex-char-code-limit*"><code>*REGEX-CHAR-CODE-LIMIT*</code></a>
1225 is high. This value will be used as the <code><i>kind</i></code>
1226 keyword argument
1227 to <a href="#create-optimized-test-function"><code>CREATE-OPTIMIZED-TEST-FUNCTION</code></a>
1228 - see there for the possible non-<code>NIL</code> values. The default
1229 value (<code>NIL</code>) should usually be fine unless you're sure
1230 that you absolutely have to optimize some character classes for speed.
1231 <p>
1232 Note: Due to the nature
1233 of <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_ld_tim.htm"><code>LOAD-TIME-VALUE</code></a>
1234 and the <a href="#compiler-macro">compiler macro for <code>SCAN</code>
1235 and other functions</a>, some scanners might be created in
1236 a <a href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_n.htm#null_lexical_environment">null
1237 lexical environment</a> at load time or at compile time so be careful
1238 to which value <code>*OPTIMIZE-CHAR-CLASSES*</code> is bound at that
1239 time.
1240 </blockquote>
1241
1242 <p><br>[Special variable]
1243 <br><a class=none name="*allow-quoting*"><b>*allow-quoting*</b></a>
1244
1245 <blockquote><br>
1246 If this value is <em>true</em> (the default is <code>NIL</code>),
1247 CL-PPCRE will support <code>\Q</code> and <code>\E</code> in regex
1248 strings to quote (disable) metacharacters. Note that this entails a
1249 slight performance penalty when creating scanners because (a copy of) the regex
1250 string is modified (probably more than once) before it
1251 is fed to the parser. Also, the parser's <a
1252 href="#ppcre-syntax-error">syntax error messages</a> will complain
1253 about the converted string and not about the original regex string.
1254
1255 <pre>
1256 * (scan &quot;^a+$&quot; &quot;a+&quot;)
1257 NIL
1258
1259 * (let ((*allow-quoting* t))
1260 <font color=orange>;;we use CREATE-SCANNER because of Lisps like SBCL that don't have an interpreter</font>
1261 (scan (create-scanner &quot;^\\Qa+\\E$&quot;) &quot;a+&quot;))
1262 0
1263 2
1264 #()
1265 #()
1266
1267 * (let ((*allow-quoting* t))
1268 (scan (create-scanner &quot;\\Qa()\\E(?#comment\\Q)a**b&quot;) &quot;()ab&quot;))
1269
1270 Quantifier '*' not allowed at position 19 in string &quot;a\\(\\)(?#commentQ)a**b&quot;
1271 </pre>
1272
1273 Note how in the last example the regex string in the error message is
1274 different from the first argument to the <code>SCAN</code>
1275 function. Also note that the second example might be easier to
1276 understand (and Lisp-ier) if you write it like this:
1277
1278 <pre>
1279 * (scan '(:sequence :start-anchor
1280 &quot;a+&quot; <font color=orange>;; no quoting necessary</font>
1281 :end-anchor)
1282 &quot;a+&quot;)
1283 0
1284 2
1285 #()
1286 #()
1287 </pre>
1288
1289 Make sure you also read <a href="#quote">the relevant section</a> in &quot;<a href="#bugs">Bugs and problems</a>.&quot;
1290 <p>
1291 Note: Due to the nature of <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_ld_tim.htm"><code>LOAD-TIME-VALUE</code></a> and the <a
1292 href="#compiler-macro">compiler macro for <code>SCAN</code> and other functions</a>, some
1293 scanners might be created in a <a
1294 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_n.htm#null_lexical_environment">null
1295 lexical environment</a> at load time or at compile time so be careful
1296 to which value <code>*ALLOW-QUOTING*</code> is bound at that
1297 time.</blockquote>
1298
1299 </blockquote>
1300
1301 <p><br>[Special variable]
1302 <br><a class=none name="*allow-named-registers*"><b>*allow-named-registers*</b></a>
1303
1304 <blockquote><br>
1305 If this value is <em>true</em> (the default is <code>NIL</code>),
1306 CL-PPCRE will support <code>(?<i>&lt;name&gt;"&lt;regex&gt;"</i>)</code> and <code>\k<i>&lt;name&gt;</i></code> in regex
1307 strings to provide named registers and back-references as in <a href="http://www.franz.com/support/documentation/7.0/doc/regexp.htm#regexp-new-capturing-2">AllegroCL</a>. <code><i>name</i></code> is has to start with a letter and can contain only alphanumeric characters or minus sign. Names of registers are matched case-sensitively.
1308 The <a href="#create-scanner2">parse tree syntax</a> is not affected by the <code>*ALLOW-NAMED-REGISTERS*</code> switch, <code>:NAMED-REGISTER</code> and <code>:BACK-REFERENCE</code> forms are always resolved as expected. There are also no restrictions on register names in this syntax except that they have to be strings.
1309
1310 <pre>
1311 <font color=orange>;; Perl compatible mode (*ALLOW-NAMED-REGISTERS* is NIL)</font>
1312 * (create-scanner "(?&lt;reg&gt;.*)")
1313 Character 'r' may not follow '(?&lt' at position 3 in string "(?&lt;reg&gt;)"
1314
1315 <font color=orange>;; just unescapes "\\k"</font>
1316 * (parse-string "\\k&lt;reg&gt;")
1317 "k&lt;reg&gt;"
1318
1319 * (setq *allow-named-registers* t)
1320 T
1321
1322 * (create-scanner "((?&lt;small&gt;[a-z]*)(?&lt;big&gt;[A-Z]*))")
1323 #&LT;CLOSURE (LAMBDA (STRING CL-PPCRE::START CL-PPCRE::END)) {AD75BFD}&gt;
1324 (NIL "small" "big")
1325
1326 <font color=orange>;; the scanner doesn't capture any information about named groups -
1327 ;; you have to store the second value returned from CREATE-SCANNER yourself</font>
1328 * (scan * "aaaBBB")
1329 0
1330 6
1331 #(0 0 3)
1332 #(6 3 6)
1333
1334 <font color=orange>;; parse tree syntax</font>
1335 * (parse-string "((?&lt;small&gt;[a-z]*)(?&lt;big&gt;[A-Z]*))")
1336 (:REGISTER
1337 (:SEQUENCE
1338 (:NAMED-REGISTER "small"
1339 (:GREEDY-REPETITION 0 NIL (:CHAR-CLASS (:RANGE #\a #\z))))
1340 (:NAMED-REGISTER "big"
1341 (:GREEDY-REPETITION 0 NIL (:CHAR-CLASS (:RANGE #\A #\Z))))))
1342
1343 * (create-scanner *)
1344 #&lt;CLOSURE (LAMBDA (STRING CL-PPCRE::START CL-PPCRE::END)) {B158E3D}&gt;
1345 (NIL "small" "big")
1346
1347 <font color=orange>;; multiple-choice back-reference</font>
1348 * (scan "^(?&lt;reg&gt;[ab])(?&lt;reg&gt;[12])\\k&lt;reg&gt;\\k&lt;reg&gt;$" "a1aa")
1349 0
1350 4
1351 #(0 1)
1352 #(1 2)
1353
1354 * (scan "^(?&lt;reg&gt;[ab])(?&lt;reg&gt;[12])\\k&lt;reg&gt;\\k&lt;reg&gt;$" "a22a")
1355 0
1356 4
1357 #(0 1)
1358 #(1 2)
1359
1360 <font color=orange>;; demonstrating most-recently-seen-register-first property of back-reference;
1361 ;; "greedy" regex (analogous to "aa?")</font>
1362 * (scan "^(?&lt;reg&gt;)(?&lt;reg&gt;a)(\\k&lt;reg&gt;)" "a")
1363 0
1364 1
1365 #(0 0 1)
1366 #(0 1 1)
1367
1368 * (scan "^(?&lt;reg&gt;)(?&lt;reg&gt;a)(\\k&lt;reg&gt;)" "aa")
1369 0
1370 2
1371 #(0 0 1)
1372 #(0 1 2)
1373
1374 <font color=orange>;; switched groups
1375 ;; "lazy" regex (analogous to "aa??")</font>
1376 * (scan "^(?&lt;reg&gt;a)(?&lt;reg&gt;)(\\k&lt;reg&gt;)" "a")
1377 0
1378 1
1379 #(0 1 1)
1380 #(1 1 1)
1381
1382 <font color=orange>;; scanner ignores the second "a"</font>
1383 * (scan "^(?&lt;reg&gt;a)(?&lt;reg&gt;)(\\k&lt;reg&gt;)" "aa")
1384 0
1385 1
1386 #(0 1 1)
1387 #(1 1 1)
1388
1389 <font color=orange>;; "aa" will be matched only when forced by adding "$" at the end</font>
1390 * (scan "^(?&lt;reg&gt;a)(?&lt;reg&gt;)(\\k&lt;reg&gt;)$" "aa")
1391 0
1392 2
1393 #(0 1 1)
1394 #(1 1 2)
1395 </pre>
1396 Note: Due to the nature of <a href="http://www.lispworks.com/documentation/HyperSpec/Body/s_ld_tim.htm"><code>LOAD-TIME-VALUE</code></a> and the <a
1397 href="#compiler-macro">compiler macro for <code>SCAN</code> and other functions</a>, some
1398 scanners might be created in a <a
1399 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_n.htm#null_lexical_environment">null
1400 lexical environment</a> at load time or at compile time so be careful
1401 to which value <code>*ALLOW-NAMED-REGISTERS*</code> is bound at that
1402 time.</blockquote>
1403 </blockquote>
1404
1405 <h4><a name="misc" class=none>Miscellaneous</a></h4>
1406
1407 <p><br>[Function]
1408 <br><a class=none name="parse-string"><b>parse-string</b> <i>string</i> =&gt; <i>parse-tree</i></a>
1409
1410 <blockquote><br> Converts the <a href="#create-scanner">regex
1411 string</a> <code><i>string</i></code> into a <a href="#create-scanner2">parse tree</a>.
1412 Note that the result is usually one possible way of creating an
1413 equivalent parse tree and not necessarily the "canonical" one.
1414 Specifically, the parse tree might contain redundant parts which are
1415 supposed to be excised when a scanner is created.
1416 </blockquote>
1417
1418 <p><br>[Function]<br><a class=none name='create-optimized-test-function'><b>create-optimized-test-function</b> <i>test-function <tt>&amp;key</tt> start end kind</i> =&gt; <i>function</i></a>
1419 <blockquote><br>
1420
1421 Given a unary test function <code><i>test-function</i></code> which is
1422 applicable to characters returns a function which yields the same
1423 boolean results for all characters with character codes
1424 from <code><i>start</i></code> to (excluding) <code><i>end</i></code>.
1425 If <code><i>kind</i></code>
1426 is <code>NIL</code>, <code><i>test-function</i></code> will simply be
1427 returned. Otherwise, <code><i>kind</i></code> should be one of:
1428 <dl>
1429 <dt><code>:HASH-TABLE</code></dt>
1430 <dd>The function builds a hash table representing all characters which
1431 satisfy the test and returns a closure which checks if a character is
1432 in that hash table.</dd>
1433 <dt><code>:CHARSET</code></dt>
1434 <dd>Instead of a hash table the function uses a &quot;charset&quot;
1435 which is a data structure using non-linear hashing and optimized to
1436 represent (sparse) sets of characters in a fast and space-efficient
1437 way (contributed by Nikodemus Siivola).</dd>
1438 <dt><code>:CHARMAP</code></dt>
1439 <dd>Instead of a hash table the function uses a bit vector to
1440 represent the set of characters.</dd>
1441 </dl>
1442 You can also use <code>:HASH-TABLE*</code> or <code>:CHARSET*</code>
1443 which are like <code>:HASH-TABLE</code> and <code>:CHARSET</code> but
1444 use the complement of the set if the set contains more than half of
1445 all characters between <code><i>start</i></code>
1446 and <code><i>end</i></code>. This saves space but needs an additional
1447 pass across all characters to create the data structure. There is no
1448 corresponding <code>:CHARMAP*</code> <code><i>kind</i></code> as the bit vectors are
1449 already created to cover the smallest possible interval which contains
1450 either the set or its complement.
1451 <p>
1452 See also <a href="#*optimize-char-classes*"><code>*OPTIMIZE-CHAR-CLASSES*</code></a>.
1453 </blockquote>
1454
1455 <p><br>[Function]
1456 <br><a class=none name="quote-meta-chars"><b>quote-meta-chars</b> <i>string</i> =&gt; <i>string'</i></a>
1457
1458 <blockquote><br>
1459 This is a simple utility function used when <a
1460 href="#*allow-quoting*"><code>*ALLOW-QUOTING*</code></a> is
1461 <em>true</em>. It returns a string <code>STRING'</code> where all
1462 non-word characters (everything except ASCII characters, digits and
1463 underline) of <code>STRING</code> are quoted by prepending a
1464 backslash similar to Perl's <code>quotemeta</code> function. It always returns a <a
1465 href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_f.htm#fresh">fresh</a>
1466 string.
1467 <pre>
1468 * (quote-meta-chars &quot;[a-z]*&quot;)
1469 &quot;\\[a\\-z\\]\\*&quot;
1470 </pre></blockquote>
1471
1472 <p><br>[Function]
1473 <br><a class=none name="regex-apropos"><b>regex-apropos</b> <i>regex <tt>&amp;optional</tt> packages <tt>&amp;key</tt> case-insensitive</i> =&gt; <i>list</i></a>
1474
1475 <blockquote><br>
1476 Like <a
1477 href="http://www.lispworks.com/documentation/HyperSpec/Body/f_apropo.htm"><code>APROPOS</code></a>
1478 but searches for interned symbols which match the regular expression
1479 <code><i>regex</i></code>. The output is implementation-dependent. If
1480 <code><i>case-insensitive</i></code> is true (which is the default)
1481 and <code><i>regex</i></code> isn't already a scanner, a
1482 case-insensitive scanner is used.
1483 <p>
1484 Here are examples for CMUCL:
1485
1486 <pre>
1487 * *package*
1488 #&lt;The COMMON-LISP-USER package, 16/21 internal, 0/9 external&gt;
1489
1490 * (defun foo (n &amp;optional (k 0)) (+ 3 n k))
1491 FOO
1492
1493 * (defparameter foo "bar")
1494 FOO
1495
1496 * (defparameter |foobar| 42)
1497 |foobar|
1498
1499 * (defparameter fooboo 43)
1500 FOOBOO
1501
1502 * (defclass frobar () ())
1503 #&lt;STANDARD-CLASS FROBAR {4874E625}&gt;
1504
1505 * (regex-apropos "foo(?:bar)?")
1506 FOO [variable] value: "bar"
1507 [compiled function] (N &amp;OPTIONAL (K 0))
1508 FOOBOO [variable] value: 43
1509 |foobar| [variable] value: 42
1510
1511 * (regex-apropos "(?:foo|fro)bar")
1512 PCL::|COMMON-LISP-USER::FROBAR class predicate| [compiled closure]
1513 FROBAR [class] #&lt;STANDARD-CLASS FROBAR {4874E625}&gt;
1514 |foobar| [variable] value: 42
1515
1516 * (regex-apropos "(?:foo|fro)bar" 'cl-user)
1517 FROBAR [class] #&lt;STANDARD-CLASS FROBAR {4874E625}&gt;
1518 |foobar| [variable] value: 42
1519
1520 * (regex-apropos "(?:foo|fro)bar" '(pcl ext))
1521 PCL::|COMMON-LISP-USER::FROBAR class predicate| [compiled closure]
1522
1523 * (regex-apropos "foo")
1524 FOO [variable] value: "bar"
1525 [compiled function] (N &amp;OPTIONAL (K 0))
1526 FOOBOO [variable] value: 43
1527 |foobar| [variable] value: 42
1528
1529 * (regex-apropos "foo" nil :case-insensitive nil)
1530 |foobar| [variable] value: 42
1531 </pre></blockquote>
1532
1533
1534
1535
1536 <p><br>[Function]
1537 <br><a class=none name="regex-apropos-list"><b>regex-apropos-list</b> <i>regex <tt>&amp;optional</tt> packages <tt>&amp;key</tt> upcase</i> =&gt; <i>list</i></a>
1538
1539 <blockquote><br>
1540 Like <a
1541 href="http://www.lispworks.com/documentation/HyperSpec/Body/f_apropo.htm"><code>APROPOS-LIST</code></a>
1542 but searches for interned symbols which match the regular expression
1543 <code><i>regex</i></code>. If <code><i>case-insensitive</i></code> is
1544 true (which is the default) and <code><i>regex</i></code> isn't
1545 already a scanner, a case-insensitive scanner is used.
1546 <p>
1547 Example (continued from above):
1548
1549 <pre>
1550 * (regex-apropos-list &quot;foo(?:bar)?&quot;)
1551 (|foobar| FOOBOO FOO)
1552 </pre></blockquote>
1553
1554 <h4><a name="conditions" class=none>Conditions</a></h4>
1555
1556 <p><br>[Condition type]
1557 <br><a class=none name="ppcre-error"><b>ppcre-error</b></a>
1558
1559 <blockquote><br>
1560 Every error signaled by CL-PPCRE is of type
1561 <code>PPCRE-ERROR</code>. This is a direct subtype of <a
1562 href="http://www.lispworks.com/documentation/HyperSpec/Body/e_smp_er.htm"><code>SIMPLE-ERROR</code></a>
1563 without any additional slots or options.
1564 </blockquote>
1565
1566 <p><br>[Condition type]
1567 <br><a class=none name="ppcre-invocation-error"><b>ppcre-invocation-error</b></a>
1568
1569 <blockquote><br>
1570 Errors of type <code>PPCRE-INVOCATION-ERROR</code>
1571 are signaled if one of the exported functions of CL-PPCRE is called with wrong or
1572 inconsistent arguments. This is a direct subtype of <a
1573 href="#ppcre-error"><code>PPCRE-ERROR</code></a> without any
1574 additional slots or options.
1575 </blockquote>
1576
1577 <p><br>[Condition type]
1578 <br><a class=none name="ppcre-syntax-error"><b>ppcre-syntax-error</b></a>
1579
1580 <blockquote><br>
1581 An error of type <code>PPCRE-SYNTAX-ERROR</code> is signaled if
1582 CL-PPCRE's parser encounters an error when trying to parse a regex
1583 string or to convert a parse tree into its internal representation.
1584 This is a direct subtype of <a
1585 href="#ppcre-error"><code>PPCRE-ERROR</code></a> with two additional
1586 slots. These denote the regex string which HTML-PPCRE was parsing and
1587 the position within the string where the error occurred. If the error
1588 happens while CL-PPCRE is converting a parse tree, both of these slots
1589 contain <code>NIL</code>. (See the next two entries on how to access
1590 these slots.)
1591 <p>
1592 As many syntax errors can't be detected before the parser is at the
1593 end of the stream, the row and column usually denote the last position
1594 where the parser was happy and not the position where it gave up.
1595
1596 <pre>
1597 * (handler-case
1598 (scan &quot;foo**x&quot; &quot;fooox&quot;)
1599 (ppcre-syntax-error (condition)
1600 (format t &quot;Houston, we've got a problem with the string ~S:~%~
1601 Looks like something went wrong at position ~A.~%~
1602 The last message we received was \&quot;~?\&quot;.&quot;
1603 (ppcre-syntax-error-string condition)
1604 (ppcre-syntax-error-pos condition)
1605 (simple-condition-format-control condition)
1606 (simple-condition-format-arguments condition))
1607 (values)))
1608 Houston, we've got a problem with the string &quot;foo**x&quot;:
1609 Looks like something went wrong at position 4.
1610 The last message we received was &quot;Quantifier '*' not allowed.&quot;.
1611 </pre>
1612 </blockquote>
1613
1614 <p><br>[Function]
1615 <br><a class=none name="ppcre-syntax-error-string"><b>ppcre-syntax-error-string</b></a> <i>condition</i> =&gt; <i>string</i>
1616
1617 <blockquote><br>
1618 If <code><i>condition</i></code> is a condition of type <a
1619 href="#ppcre-syntax-error"><code>PPCRE-SYNTAX-ERROR</code></a>, this
1620 function will return the string the parser was parsing when the error was
1621 encountered (or <code>NIL</code> if the error happened while trying to
1622 convert a parse tree). This might be particularly useful when <a
1623 href="#*allow-quoting*"><code>*ALLOW-QUOTING*</code></a> is
1624 <em>true</em> because in this case the offending string might not be the one you gave to the <a
1625 href="#create-scanner"><code>CREATE-SCANNER</code></a> function.
1626 </blockquote>
1627
1628 <p><br>[Function]
1629 <br><a class=none name="ppcre-syntax-error-pos"><b>ppcre-syntax-error-pos</b></a> <i>condition</i> =&gt; <i>number</i>
1630
1631 <blockquote><br>
1632 If <code><i>condition</i></code> is a condition of type <a
1633 href="#ppcre-syntax-error"><code>PPCRE-SYNTAX-ERROR</code></a>, this
1634 function will return the position within the string where the error
1635 occurred (or <code>NIL</code> if the error happened while trying to
1636 convert a parse tree).
1637 </blockquote>
1638
1639 <br>&nbsp;<br><h3><a name="unicode" class=none>Unicode properties</a></h3>
1640
1641 You can add support for Unicode properties to CL-PPCRE by loading
1642 the CL-PPCRE-UNICODE system (which depends on <a href="http://weitz.de/cl-unicode/">CL-UNICODE</a>):
1643 <pre>
1644 (asdf:oos 'asdf:load-op :cl-ppcre-unicode)
1645 </pre>
1646 This will automatically
1647 install <a href="#unicode-property-resolver"><code>UNICODE-PROPERTY-RESOLVER</code></a>
1648 as your <a href="#*property-resolver*">property resolver</a>.
1649 <p>
1650 See the <a href="http://weitz.de/cl-unicode/">CL-UNICODE</a>
1651 documentation for information about the supported Unicode properties
1652 and how they are named.
1653
1654 <p><br>[Function]<br><a class=none name='unicode-property-resolver'><b>unicode-property-resolver</b> <i>property-name</i> =&gt; <i>function-or-nil</i></a>
1655 <blockquote><br>
1656 A <a href="#*property-resolver*">property
1657 resolver</a> which understands Unicode properties using
1658 <a href="http://weitz.de/cl-unicode/">CL-UNICODE</a>'s <a href="http://weitz.de/cl-unicode/#property-test"><code>PROPERTY-TEST</code></a>
1659 function. This resolver is automatically installed
1660 in <a href="#*property-resolver*"><code>*PROPERTY-RESOLVER*</code></a>
1661 when the <a href="#unicode">CL-PPCRE-UNICODE</a> system is loaded.
1662 <pre>
1663 * (scan-to-strings "\\p{Script:Latin}+" "0+AB_*")
1664 "AB"
1665 #()
1666 </pre>
1667 Note that this symbol is exported from
1668 the <code>CL-PPCRE-UNICODE</code> package and not from
1669 the <code>CL-PPCRE</code> package.
1670 </blockquote>
1671
1672
1673 <br>&nbsp;<br><h3><a name="filters" class=none>Filters</a></h3>
1674
1675 Because several users have asked for it, CL-PPCRE now offers
1676 &quot;filters&quot; (see <a href="#filterdef">above</a> for syntax)
1677 which are basically arbitrary, user-defined functions that can act as
1678 regex building blocks. Filters can only be used within <a
1679 href="#create-scanner2">parse trees</a>, not within Perl regex
1680 strings.
1681 <p>
1682 A filter is defined by its <em>filter function</em> which must be a
1683 function of one argument. During the parsing process this function
1684 might be called once or several times or it might not be called at
1685 all. If it's called, its argument is an integer <code><i>pos</i></code>
1686 which is the current position within the target string. The filter can
1687 either return <code>NIL</code> (which means that the subexpression
1688 represented by this filter didn't match) or an integer not smaller
1689 than <code><i>pos</i></code> for success. A zero-length assertion
1690 should return <code><i>pos</i></code> itself while a filter which
1691 wants to consume <code>N</code> characters should return
1692 <code>(+&nbsp;POS&nbsp;N)</code>.
1693 <p>
1694 If you supply the optional value <code><i>length</i></code> and it is
1695 not <code>NIL</code>, then this is a promise to the regex engine that
1696 your filter will <em>always</em> consume <em>exactly</em>
1697 <code><i>length</i></code> characters. The regex engine might use this
1698 information for optimization purposes but it is otherwise irrelevant
1699 to the outcome of the matching process.
1700 <p>
1701 The filter function can access the following special variables from
1702 its code body:
1703 <dl>
1704
1705 <dt><code>CL-PPCRE::*STRING*</code></dt>
1706 <dd>The target (a string) of the current matching process.</dd>
1707
1708 <dt><code>CL-PPCRE::*START-POS*</code> and
1709 <code>CL-PPCRE::*END-POS*</code></dt>
1710 <dd>The start and end (integers) indices
1711 of the current matching process. These correspond to the
1712 <code>START</code> and <code>END</code> keyword parameters
1713 of <a href="#scan"><code>SCAN</code></a>.</dd>
1714
1715 <dt><code>CL-PPCRE::*REAL-START-POS*</code></dt>
1716 <dd>The initial starting
1717 position. This is only relevant for repeated scans (as in <a
1718 href="#do-scans"><code>DO-SCANS</code></a>) where
1719 <code>CL-PPCRE::*START-POS*</code> will be moved forward while
1720 <code>CL-PPCRE::*REAL-START-POS*</code> won't. For normal scans the
1721 value of this variable is <code>NIL</code>.</dd>
1722
1723 <dt><CODE>CL-PPCRE::*REG-STARTS*</CODE> and
1724 <CODE>CL-PPCRE::*REG-ENDS*</CODE></dt>
1725 <dd>Two simple vectors which denote the
1726 start and end indices of registers within the regular expression. The
1727 first register is indexed by&nbsp;0. If a register hasn't matched yet,
1728 then its corresponding entry in <CODE>CL-PPCRE::*REG-STARTS*</CODE> is
1729 <code>NIL</code>.</dd>
1730
1731 </dl>
1732
1733 These variables should be considered read-only. Do <em>not</em> change
1734 these values unless you really know what you're doing!
1735 <p>
1736 Note that the names of the variables are not exported from the
1737 <code>CL-PPCRE</code> package because there's no explicit guarantee
1738 that they will be available in future releases. (Although after so
1739 many years it is <em>very</em> unlikely that they'll go away...)
1740 <pre>
1741 * (defun my-info-filter (pos)
1742 &quot;Show some info about the matching process.&quot;
1743 (format t &quot;Called at position ~A~%&quot; pos)
1744 (loop with dim = (array-dimension cl-ppcre::*reg-starts* 0)
1745 for i below dim
1746 for reg-start = (aref cl-ppcre::*reg-starts* i)
1747 for reg-end = (aref cl-ppcre::*reg-ends* i)
1748 do (format t &quot;Register ~A is currently &quot; (1+ i))
1749 when reg-start
1750 (write-string cl-ppcre::*string* nil
1751 do (write-char #\')
1752 (write-string cl-ppcre::*string* nil
1753 :start reg-start :end reg-end)
1754 (write-char #\')
1755 else
1756 do (write-string &quot;unbound&quot;)
1757 do (terpri))
1758 (terpri)
1759 pos)
1760 MY-INFO-FILTER
1761
1762 * (scan '(:sequence
1763 (:register
1764 (:greedy-repetition 0 nil
1765 (:char-class (:range #\a #\z))))
1766 (:filter my-info-filter 0) &quot;X&quot;)
1767 &quot;bYcdeX&quot;)
1768 Called at position 1
1769 Register 1 is currently 'b'
1770
1771 Called at position 0
1772 Register 1 is currently ''
1773
1774 Called at position 1
1775 Register 1 is currently ''
1776
1777 Called at position 5
1778 Register 1 is currently 'cde'
1779
1780 2
1781 6
1782 #(2)
1783 #(5)
1784
1785 * (scan '(:sequence
1786 (:register
1787 (:greedy-repetition 0 nil
1788 (:char-class (:range #\a #\z))))
1789 (:filter my-info-filter 0) &quot;X&quot;)
1790 &quot;bYcdeZ&quot;)
1791 NIL
1792
1793 * (defun my-weird-filter (pos)
1794 &quot;Only match at this point if either pos is odd and the character
1795 we're looking at is lowercase or if pos is even and the next two
1796 characters we're looking at are uppercase. Consume these characters if
1797 there's a match.&quot;
1798 (format t &quot;Trying at position ~A~%&quot; pos)
1799 (cond ((and (oddp pos)
1800 (&lt; pos cl-ppcre::*end-pos*)
1801 (lower-case-p (char cl-ppcre::*string* pos)))
1802 (1+ pos))
1803 ((and (evenp pos)
1804 (&lt; (1+ pos) cl-ppcre::*end-pos*)
1805 (upper-case-p (char cl-ppcre::*string* pos))
1806 (upper-case-p (char cl-ppcre::*string* (1+ pos))))
1807 (+ pos 2))
1808 (t nil)))
1809 MY-WEIRD-FILTER
1810
1811 * (defparameter *weird-regex*
1812 `(:sequence &quot;+&quot; (:filter ,#'my-weird-filter) &quot;+&quot;))
1813 *WEIRD-REGEX*
1814
1815 * (scan *weird-regex* &quot;+A++a+AA+&quot;)
1816 Trying at position 1
1817 Trying at position 3
1818 Trying at position 4
1819 Trying at position 6
1820 5
1821 9
1822 #()
1823 #()
1824
1825 * (fmakunbound 'my-weird-filter)
1826 MY-WEIRD-FILTER
1827
1828 * (scan *weird-regex* &quot;+A++a+AA+&quot;)
1829 Trying at position 1
1830 Trying at position 3
1831 Trying at position 4
1832 Trying at position 6
1833 5
1834 9
1835 #()
1836 #()
1837 </pre>
1838
1839 Note that in the second call to <code>SCAN</code> our filter wasn't
1840 invoked at all - it was optimized away by the regex engine because it
1841 knew that it couldn't match. Also note that <code>*WEIRD-REGEX*</code>
1842 still worked after we removed the global function definition of
1843 <code>MY-WEIRD-FILTER</code> because the regular expression had
1844 captured the original definition.
1845
1846 <p>
1847
1848 For more ideas about what you can do with filters see <a
1849 href="http://common-lisp.net/pipermail/cl-ppcre-devel/2004-October/000069.html">this
1850 thread</a> on the <a href="#mail">mailing list</a>.
1851
1852 <br>&nbsp;<br><h3><a name="perl" class=none>Compatibility with Perl</a></h3>
1853
1854 Depending on your Perl version you might encounter a couple of small
1855 incompatibilities with Perl most of which aren't due to CL-PPCRE:
1856
1857 <h4><a name="empty" class=none>Empty strings instead of <code>undef</code> in <code>$1</code>, <code>$2</code>, etc.</a></h4>
1858
1859 (Cf. case #629 of <a href="#test"><code>perltestdata</code></a>.)
1860 This is <a
1861 href="http://groups.google.com/groups?threadm=87u1kw8hfr.fsf%40dyn164.dbdmedia.de">a
1862 bug</a> in Perl 5.6.1 and earlier which has been fixed in 5.8.0.
1863
1864 <h4><a name="scope" class=none>Strange scoping of embedded modifiers</a></h4>
1865
1866 (Cf. case #430 of <a href="#test"><code>perltestdata</code></a>.)
1867 This is <a
1868 href="http://groups.google.com/groups?threadm=871y80dpqh.fsf%40bird.agharta.de">a
1869 bug</a> in Perl 5.6.1 and earlier which has been fixed in 5.8.0.
1870
1871 <h4><a name="inconsistent" class=none>Inconsistent capturing of <code>$1</code>, <code>$2</code>, etc.</a></h4>
1872
1873 (Cf. case #662 of <a href="#test"><code>perltestdata</code></a>.)
1874 This is <a
1875 href="http://bugs6.perl.org/rt2/Ticket/Display.html?id=18708">a
1876 bug</a> in Perl which hasn't been fixed yet.
1877
1878 <h4><a name="lookaround" class=none>Captured groups not available outside of look-aheads and look-behinds</a></h4>
1879
1880 (Cf. case #1439 of <a href="#test"><code>perltestdata</code></a>.)
1881 Well, OK, this ain't a Perl bug. I just can't quite understand why
1882 captured groups should only be seen within the scope of a look-ahead
1883 or look-behind. For the moment, CL-PPCRE and Perl agree to
1884 disagree... :)
1885
1886 <h4><a name="order" class=none>Alternations don't always work from left to right</a></h4>
1887
1888 (Cf. case #790 of <a href="#test"><code>perltestdata</code></a>.) I
1889 also think this a Perl bug but I currently have lost the drive to
1890 report it.
1891
1892 <h4><a name="uprops" class=none>Different names for Unicode properties</a></h4>
1893
1894 The names of <a href="#unicode">Unicode properties</a> are derived
1895 from <a href="http://weitz.de/cl-unicode/">CL-UNICODE</a> and might
1896 differ slightly from the names in Perl. Most of them should be
1897 identical, though.
1898 Also, <a href="http://weitz.de/cl-unicode/">CL-UNICODE</a> is based on
1899 Unicode&nbsp;5.1 while your installed Perl version might be not.
1900
1901 <h4><a name="mac" class=none><code>&quot;\r&quot;</code> doesn't work with MCL</a></h4>
1902
1903 (Cf. case #9 of <a href="#test"><code>perltestdata</code></a>.) For
1904 some strange reason that I don't understand MCL translates
1905 <code>#\Return</code> to <code>(CODE-CHAR 10)</code> while MacPerl
1906 translates <code>&quot;\r&quot;</code> to <code>(CODE-CHAR
1907 13)</code>. Hmmm...
1908
1909 <h4><a name="alpha" class=none>What about <code>&quot;\w&quot;</code>?</a></h4>
1910
1911 CL-PPCRE uses <a
1912 href="http://www.lispworks.com/documentation/HyperSpec/Body/f_alphan.htm"><code>ALPHANUMERICP</code></a>
1913 to decide whether a character matches Perl's
1914 <code>&quot;\w&quot;</code>, so depending on your CL implementation
1915 you might encounter differences between Perl and CL-PPCRE when
1916 matching non-ASCII characters.
1917
1918 <br>&nbsp;<br><h3><a name="bugs" class=none>Bugs and problems</a></h3>
1919
1920 <h4><a name="quote" class=none><code>&quot;\Q&quot;</code> doesn't work, or does it?</a></h4>
1921
1922 In Perl the following code works as expected, i.e. it prints <code>1</code>.
1923 <pre>
1924 #!/usr/bin/perl -l
1925
1926 $a = '\E*';
1927 print 1
1928 if '\E*\E*' =~ /(?:\Q$a\E){2}/;
1929 </pre>
1930
1931 If you try to do something similar in CL-PPCRE, you get an error:
1932
1933 <pre>
1934 * (let ((*allow-quoting* t)
1935 (a &quot;\\E*&quot;))
1936 (scan (concatenate 'string &quot;(?:\\Q&quot; a &quot;\\E){2}&quot;) &quot;\\E*\\E*&quot;))
1937 Quantifier '*' not allowed at position 3 in string &quot;(?:*\\E){2}&quot;
1938 </pre>
1939
1940 The error message might give you a hint as to why this happens:
1941 Because <a href="#*allow-quoting*"><code>*ALLOW-QUOTING*</code></a>
1942 was <em>true</em> the concatenated string was pre-processed before it
1943 was fed to CL-PPCRE's parser - the result of this pre-processing is
1944 <code>&quot;(?:*\\E){2}&quot;</code> because the
1945 <code>&quot;\\E&quot;</code> in the string <code>A</code> was taken to
1946 be the end of the quoted section started by
1947 <code>&quot;\\Q&quot;</code>. This cannot happen in Perl due to its
1948 complicated interpolation rules - see <code>man&nbsp;perlop</code> for
1949 the scary details. It <em>can</em> happen in CL-PPCRE, though.
1950 Bummer!
1951 <p>
1952 What gives? <code>&quot;\\Q...\\E&quot;</code> in CL-PPCRE should only
1953 be used in literal strings. If you want to quote arbitrary strings,
1954 try <a href="http://weitz.de/cl-interpol/">CL-INTERPOL</a> or use <a
1955 href="#quote-meta-chars"><code>QUOTE-META-CHARS</code></a>:
1956 <pre>
1957 * (let ((a &quot;\\E*&quot;))
1958 (scan (concatenate 'string &quot;(?:&quot; (quote-meta-chars a) &quot;){2}&quot;) &quot;\\E*\\E*&quot;))
1959 0
1960 6
1961 #()
1962 #()
1963 </pre>
1964 Or, even better and Lisp-ier, use the <a href="#create-scanner2">S-expression syntax</a> instead - no need for quoting in this case:
1965 <pre>
1966 * (let ((a "\\E*"))
1967 (scan `(:greedy-repetition 2 2 ,a) "\\E*\\E*"))
1968 0
1969 6
1970 #()
1971 #()
1972 </pre>
1973
1974 <h4><a name="backslash" class=none>Backslashes may confuse you...</a></h4>
1975
1976 <pre>
1977 * (let ((a &quot;y\\y&quot;))
1978 (scan a a))
1979 NIL
1980 </pre>
1981
1982 You didn't expect this to yield <code>NIL</code>, did you? Shouldn't something like <code>(SCAN&nbsp;A&nbsp;A)</code> always return a true value? No, because the first and the second argument to <code>SCAN</code> are handled differently: The first argument is fed to CL-PPCRE's parser and is treated like a Perl regular expression. In particular, the parser "sees" <code>\y</code> and converts it to <code>y</code> because <code>\y</code> has no special meaning in regular expressions. So, the regular expression is the constant string <code>"yy"</code>. But the second argument isn't converted - it is left as is, i.e. it's equivalent to Perl's <code>'y\y'</code>. In other words, this example would be equivalent to the Perl code
1983
1984 <pre>
1985 'y\y' =~ /y\y/;
1986 </pre>
1987
1988 or to
1989
1990 <pre>
1991 $a = 'y\y';
1992 $a =~ /$a/;
1993 </pre>
1994
1995 which should explain why it doesn't match.
1996 <p>
1997 Still confused? You might want to try <a href="http://weitz.de/cl-interpol/">CL-INTERPOL</a>.
1998
1999 <br>&nbsp;<br><h3><a class=none name="allegro">AllegroCL compatibility mode</a></h3>
2000
2001 Since autumn 2004 <a
2002 href="http://www.franz.com/products/allegrocl/">AllegroCL</a> offers
2003 <a
2004 href="http://www.franz.com/support/documentation/7.0/doc/regexp.htm">a
2005 new regular expression API</a> with a syntax very similar to
2006 CL-PPCRE. Although CL-PPCRE is quite fast already, AllegroCL's engine will
2007 most likely be even faster (but only on AllegroCL, of course). However, you might want to
2008 stick to CL-PPCRE because you have a "legacy" application or because
2009 you want your code to be portable to other Lisp implementations.
2010 Therefore, beginning from version 1.2.0, CL-PPCRE offers a
2011 "compatibility mode" where you can continue using the CL-PPCRE API as
2012 described <a href="#dict">above</a> but deploy the AllegroCL regex
2013 engine under the hood. (The details are: Calls to <a
2014 href="#create-scanner"><code>CREATE-SCANNER</code></a> and <a
2015 href="#scan"><code>SCAN</code></a> are dispatched to their AllegroCL
2016 counterparts <a
2017 href="http://www.franz.com/support/documentation/7.0/doc/operators/excl/compile-re.htm"><code>EXCL:COMPILE-RE</code></a>
2018 and <a
2019 href="http://www.franz.com/support/documentation/7.0/doc/operators/excl/match-re.htm"><code>EXCL:MATCH-RE</code></a>
2020 while everything else is left as is.)
2021 <p>
2022 The advantage of this mode is that you'll get a much smaller image and
2023 most likely faster code. (But note that CL-PPCRE needs to do a small amount of work to massage AllegroCL's output into the format expected by CL-PPCRE.) The downside is that your code won't be
2024 fully compatible with CL-PPCRE anymore. Here are some of the
2025 differences (most of which probably don't matter very often):
2026 <ul>
2027 <li>The AllegroCL engine doesn't offer <a
2028 href="#parse-tree-synonym">parse tree synonyms</a> and <a href="#filters">filters</a>.
2029 <li>The AllegroCL engine <a href="http://www.franz.com/support/documentation/8.0/doc/regexp.htm#regexp-new-compatibility-2">will choke on some regular expressions involving curly braces</a> that are accepted by Perl and CL-PPCRE's native engine.
2030 <li>The AllegroCL engine's case-folding mode switch (which is used instead of CL-PPCRE's <a href="#create-scanner"><code>:CASE-INSENSITIVE</code> keyword parameter</a>) <a href="http://www.franz.com/support/documentation/8.0/doc/regexp.htm#regexp-new-matching-2">is currently only effective for ASCII characters</a>.
2031 <li>The AllegroCL engine <a href="http://www.franz.com/support/documentation/8.0/doc/regexp.htm#regexp-new-compatibility-2">doesn't support</a> <a href="#*allow-quoting*">quoting of metacharacters</a>.
2032 <li>In AllegroCL compatibility mode compiled regular expressions (as returned by <a href="#create-scanner"><code>CREATE-SCANNER</code></a>) aren't functions but structures.
2033 <li>The AllegroCL engine <a href="http://www.franz.com/support/documentation/8.0/doc/regexp.htm#regexp-new-compatibility-2">doesn't support</a> <a href="#*property-resolver*">named properties</a>.
2034 </ul>
2035 For more details about the AllegroCL engine and possible deviations from CL-PPCRE see the <a href="http://www.franz.com/support/documentation/8.0/doc/regexp.htm">documentation</a> at the <a href="http://www.franz.com/">Franz Inc. website</a>.
2036 <p>
2037 To use the AllegroCL compatibility mode you have to
2038 <pre>
2039 (push :use-acl-regexp2-engine *features*)
2040 </pre>
2041 <em>before</em> you compile CL-PPCRE.
2042
2043 <br>&nbsp;<br><h3><a class=none name="blabla">Hints, comments, performance considerations</a></h3>
2044
2045 Here are, in no particular order, a couple of things about CL-PPCRE
2046 and regular expressions in general that you might or might not want to
2047 read.
2048
2049 <ul>
2050 <li>A lot of hackers (especially users of Perl and other scripting
2051 languages) think that regular expressions are the greatest thing
2052 since sliced bread and use it for almost everything. That is just
2053 plain wrong. Other hackers (especially Lispers) tend to think that
2054 regular expressions are the work of the devil and try to avoid them
2055 at all cost. That's also wrong. Regular expressions are a handy
2056 and useful addition to your toolkit which you should use when
2057 appropriate - you should just try to figure out first <em>if</em>
2058 they're appropriate for the task at hand.
2059
2060 <li>If you're concerned about the string syntax of regular
2061 expressions which can look like line noise and is really hard to
2062 read for long expressions, consider using
2063 CL-PPCRE's <a href="#create-scanner2">S-expression syntax</a>
2064 instead. It is less error-prone and you don't have to worry about
2065 escaping characters. It is also easier to manipulate
2066 programmatically.
2067
2068 <li>For alternations, order is important. The general rule is that
2069 the regex engine tries from left to right and tries to match as much
2070 as possible.
2071 <pre>
2072 CL-USER 1 > (scan-to-strings "<=|<" "<=")
2073 "<="
2074 #()
2075
2076 CL-USER 2 > (scan-to-strings "<|<=" "<=")
2077 "<"
2078 #()
2079 </pre>
2080
2081 <li><a class=none name="compiler-macro">CL-PPCRE</a>
2082 uses <a href="http://www.lispworks.com/documentation/HyperSpec/Body/03_bba.htm">compiler
2083 macros</a> to pre-compile scanners
2084 at <a href=="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_l.htm#load_time">load
2085 time</a> if possible. This happens if the compiler can determine
2086 that the regular expression (no matter if it's a string or an
2087 S-expression)
2088 is <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_consta.htm">constant</a>
2089 at <a href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_c.htm#compile_time">compile
2090 time</a> and is intended to save the time for creating scanners
2091 at <a href="http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_e.htm#execution_time">execution
2092 time</a> (probably creating the same scanner over and over in a
2093 loop). Make sure you don't prevent the compiler from helping you.
2094 For example, a definition like this one is usually not a good idea:
2095 <pre>
2096 (defun regex-match (regex target)
2097 <font color=orange>;; don't do that!</font>
2098 (scan regex target))
2099 </pre>
2100
2101 <li>If you want to search for a substring in a large string or if
2102 you search for the same string very
2103 often, <a href="#scan"><code>SCAN</code></a> will usually be faster
2104 than Common
2105 Lisp's <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_search.htm"><code>SEARCH</code></a>
2106 if you <a href="#*use-bmh-matchers*">use BMH matchers</a>. However,
2107 this only makes sense if scanner creation time is not the
2108 limiting factor, i.e. if the search target is <em>very</em> large or
2109 if you're using the same scanner very often.
2110
2111 <li>Complementary to the last hint, <em>don't</em> use regular
2112 expressions for one-time searches for constant strings. That's a
2113 terrible waste of resources.
2114
2115 <li><a href="#*use-bmh-matchers*"><code>*USE-BMH-MATCHERS*</code></a> together with a large value for
2116 <a href="#*regex-char-code-limit*"><code>*REGEX-CHAR-CODE-LIMIT*</code></a>
2117 can lead to huge scanners.
2118
2119 <li>A character class is by default translated into a sequence of
2120 tests exactly as you might expect. For
2121 example, <code>"[af-l\\d]"</code> means to test if the character is
2122 equal to <code>#\a</code>, then to test if it's
2123 between <code>#\f</code> and <code>#\l</code>, then if it's a digit.
2124 There's by default no attempt to remove redundancy (as
2125 in <code>"[a-ge-kf]"</code>) or to otherwise optimize these tests
2126 for speed. However, you can play
2127 with <a href="#*optimize-char-classes*"><code>*OPTIMIZE-CHAR-CLASSES*</code></a>
2128 if you've identified character classes as a bottleneck and want to
2129 make sure that you have <em>O(1)</em> test functions.
2130
2131 <li>If you know that the expression you're looking for is anchored,
2132 use anchors in your regex. This can help the engine a lot to make
2133 your scanners more efficient.
2134
2135 <li>In addition to anchors, constant strings at the start or end of a
2136 regular expression can help the engine to quickly scan a string.
2137 Note that for example <code>"(a-d|aebf)"</code>
2138 and <code>"ab(cd|ef)"</code> are equivalent, but only the second
2139 form has a constant start the regex engine can recognize.
2140
2141 <li>Try to avoid alternations if possible or at least factor them
2142 out as in the example above.
2143
2144 <li>If neither anchors nor constant strings are in sight, maybe
2145 "standalone" (sometimes also called "possessive") regular
2146 expressions can be helpful. Try the following:
2147 <pre>
2148 (let ((target (make-string 10000 :initial-element #\a))
2149 (scanner-1 (create-scanner "a*\\d"))
2150 (scanner-2 (create-scanner "(?>a*)\\d")))
2151 (time (scan scanner-1 target))
2152 (time (scan scanner-2 target)))
2153 </pre>
2154
2155 <li>Consider using <a href="#create-scanner">"single-line mode"</a>
2156 if it makes sense for your task. By default (following Perl's
2157 practice), a dot means to search for any character <em>except</em>
2158 line breaks. In single-line mode a dot searches for <em>any</em>
2159 character which in some cases means that large parts of the target
2160 can actually be skipped. This can be vastly more efficient for
2161 large targets.
2162
2163 <li>Don't use capturing register groups where a non-capturing group
2164 would do, i.e. <em>only</em> use registers if you need to refer to
2165 them later. If you use a register, each scan process needs to
2166 allocate space for it and update its contents (possibly many times)
2167 until it's finished. (In Perl parlance - use <code>"(?:foo)"</code> instead of
2168 <code>"(foo)"</code> whenever possible.)
2169
2170 <li>In addition to what has been said in the last hint, note that
2171 Perl semantics force the regex engine to report the <em>last</em>
2172 match for each register. This implies for example
2173 that <code>"([a-c])+"</code> and <code>"[a-c]*([a-c])"</code> have
2174 exactly the same semantics but completely different performance
2175 characteristics. (Actually, in some cases CL-PPCRE automatically
2176 converts expressions from the first type into the second type.
2177 That's not always possible, though, and you shouldn't rely on it.)
2178
2179 <li>By default, repetitions are "greedy" in Perl (and thus in
2180 CL-PPCRE). This has an impact on performance and also on the actual
2181 outcome of a scan. Look at your repetitions and ponder if a greedy
2182 repetition is really what you want.
2183 </ul>
2184
2185 <br>&nbsp;<br><h3><a class=none name="ack">Acknowledgements</a></h3>
2186
2187 Although I didn't use their code, I was heavily inspired by looking at
2188 the Scheme/CL regex implementations of <a
2189 href="http://www.ccs.neu.edu/home/dorai/pregexp/pregexp.html">Dorai
2190 Sitaram</a> and <a
2191 href="http://www.geocities.com/mparker762/clawk#regex">Michael
2192 Parker</a>. Also, the nice folks from CMUCL's <a
2193 href="http://www.cons.org/cmucl/support.html">mailing list</a> as well
2194 as the output of Perl's <code>use re &quot;debug&quot;</code> pragma
2195 have been very helpful in optimizing the scanners created by CL-PPCRE.
2196
2197 <p>The list of people who participated in this project in one way or
2198 the other has grown too long to maintain it here. See
2199 the <a href="http://weitz.de/cl-ppcre/CHANGELOG">ChangeLog</a> for all
2200 the people who helped with patches, bug reports, or in other ways.
2201 Thanks to all of them!
2202
2203 <p>
2204 Thanks to the guys at
2205 &quot;<a href="http://www.weinhandel-ottensen.de/">Caf&eacute;
2206 Ol&eacute;</a>&quot;
2207 in <a href="http://en.wikipedia.org/wiki/Hamburg">Hamburg</a> where I
2208 wrote most of the 0.1.0&nbsp;release and thanks to my wife for lending
2209 me her PowerBook to test early versions of CL-PPCRE with MCL and
2210 OpenMCL.
2211
2212 <p>
2213 $Header: /usr/local/cvsrep/cl-ppcre/doc/index.html,v 1.200 2009/10/28 07:36:31 edi Exp $
2214 <p><a href="http://weitz.de/index.html">BACK TO MY HOMEPAGE</a>
2215
2216 </body>
2217 </html>