287 |
of the interpreter is not unnecessarily increased. |
of the interpreter is not unnecessarily increased. |
288 |
|
|
289 |
|
|
290 |
|
@page |
291 |
|
@node Rx Regexps |
292 |
|
@chapter The Rx Regular Expression Library |
293 |
|
|
294 |
|
[FIXME: this is taken from Gary and Mark's quick summaries and should be |
295 |
|
reviewed and expanded. Rx is pretty stable, so could already be done!] |
296 |
|
|
297 |
|
@cindex rx |
298 |
|
@cindex finite automaton |
299 |
|
|
300 |
|
The @file{guile-lang-allover} package provides an interface to Tom |
301 |
|
Lord's Rx library (currently only to POSIX regular expressions). Use of |
302 |
|
the library requires a two step process: compile a regular expression |
303 |
|
into an efficient structure, then use the structure in any number of |
304 |
|
string comparisons. |
305 |
|
|
306 |
|
For example, given the regular expression @samp{abc.} (which matches any |
307 |
|
string containing @samp{abc} followed by any single character): |
308 |
|
|
309 |
|
@smalllisp |
310 |
|
guile> @kbd{(define r (regcomp "abc."))} |
311 |
|
guile> @kbd{r} |
312 |
|
#<rgx abc.> |
313 |
|
guile> @kbd{(regexec r "abc")} |
314 |
|
#f |
315 |
|
guile> @kbd{(regexec r "abcd")} |
316 |
|
#((0 . 4)) |
317 |
|
guile> |
318 |
|
@end smalllisp |
319 |
|
|
320 |
|
The definitions of @code{regcomp} and @code{regexec} are as follows: |
321 |
|
|
322 |
|
@deffn {Scheme Procedure} regcomp pattern [flags] |
323 |
|
Compile the regular expression pattern using POSIX rules. Flags is |
324 |
|
optional and should be specified using symbolic names: |
325 |
|
@defvar REG_EXTENDED |
326 |
|
use extended POSIX syntax |
327 |
|
@end defvar |
328 |
|
@defvar REG_ICASE |
329 |
|
use case-insensitive matching |
330 |
|
@end defvar |
331 |
|
@defvar REG_NEWLINE |
332 |
|
allow anchors to match after newline characters in the |
333 |
|
string and prevents @code{.} or @code{[^...]} from matching newlines. |
334 |
|
@end defvar |
335 |
|
|
336 |
|
The @code{logior} procedure can be used to combine multiple flags. |
337 |
|
The default is to use |
338 |
|
POSIX basic syntax, which makes @code{+} and @code{?} literals and @code{\+} |
339 |
|
and @code{\?} |
340 |
|
operators. Backslashes in @var{pattern} must be escaped if specified in a |
341 |
|
literal string e.g., @code{"\\(a\\)\\?"}. |
342 |
|
@end deffn |
343 |
|
|
344 |
|
@deffn {Scheme Procedure} regexec regex string [match-pick] [flags] |
345 |
|
Match @var{string} against the compiled POSIX regular expression |
346 |
|
@var{regex}. |
347 |
|
@var{match-pick} and @var{flags} are optional. Possible flags (which can be |
348 |
|
combined using the logior procedure) are: |
349 |
|
|
350 |
|
@defvar REG_NOTBOL |
351 |
|
The beginning of line operator won't match the beginning of |
352 |
|
@var{string} (presumably because it's not the beginning of a line) |
353 |
|
@end defvar |
354 |
|
|
355 |
|
@defvar REG_NOTEOL |
356 |
|
Similar to REG_NOTBOL, but prevents the end of line operator |
357 |
|
from matching the end of @var{string}. |
358 |
|
@end defvar |
359 |
|
|
360 |
|
If no match is possible, regexec returns #f. Otherwise @var{match-pick} |
361 |
|
determines the return value: |
362 |
|
|
363 |
|
@code{#t} or unspecified: a newly-allocated vector is returned, |
364 |
|
containing pairs with the indices of the matched part of @var{string} and any |
365 |
|
substrings. |
366 |
|
|
367 |
|
@code{""}: a list is returned: the first element contains a nested list |
368 |
|
with the matched part of @var{string} surrounded by the the unmatched parts. |
369 |
|
Remaining elements are matched substrings (if any). All returned |
370 |
|
substrings share memory with @var{string}. |
371 |
|
|
372 |
|
@code{#f}: regexec returns #t if a match is made, otherwise #f. |
373 |
|
|
374 |
|
vector: the supplied vector is returned, with the first element replaced |
375 |
|
by a pair containing the indices of the matched portion of @var{string} and |
376 |
|
further elements replaced by pairs containing the indices of matched |
377 |
|
substrings (if any). |
378 |
|
|
379 |
|
list: a list will be returned, with each member of the list |
380 |
|
specified by a code in the corresponding position of the supplied list: |
381 |
|
|
382 |
|
a number: the numbered matching substring (0 for the entire match). |
383 |
|
|
384 |
|
@code{#\<}: the beginning of @var{string} to the beginning of the part matched |
385 |
|
by regex. |
386 |
|
|
387 |
|
@code{#\>}: the end of the matched part of @var{string} to the end of |
388 |
|
@var{string}. |
389 |
|
|
390 |
|
@code{#\c}: the "final tag", which seems to be associated with the "cut |
391 |
|
operator", which doesn't seem to be available through the posix |
392 |
|
interface. |
393 |
|
|
394 |
|
e.g., @code{(list #\< 0 1 #\>)}. The returned substrings share memory with |
395 |
|
@var{string}. |
396 |
|
@end deffn |
397 |
|
|
398 |
|
Here are some other procedures that might be used when using regular |
399 |
|
expressions: |
400 |
|
|
401 |
|
@deffn {Scheme Procedure} compiled-regexp? obj |
402 |
|
Test whether obj is a compiled regular expression. |
403 |
|
@end deffn |
404 |
|
|
405 |
|
@deffn {Scheme Procedure} regexp->dfa regex [flags] |
406 |
|
@end deffn |
407 |
|
|
408 |
|
@deffn {Scheme Procedure} dfa-fork dfa |
409 |
|
@end deffn |
410 |
|
|
411 |
|
@deffn {Scheme Procedure} reset-dfa! dfa |
412 |
|
@end deffn |
413 |
|
|
414 |
|
@deffn {Scheme Procedure} dfa-final-tag dfa |
415 |
|
@end deffn |
416 |
|
|
417 |
|
@deffn {Scheme Procedure} dfa-continuable? dfa |
418 |
|
@end deffn |
419 |
|
|
420 |
|
@deffn {Scheme Procedure} advance-dfa! dfa string |
421 |
|
@end deffn |
422 |
|
|
423 |
|
|
424 |
@c Local Variables: |
@c Local Variables: |
425 |
@c TeX-master: "guile.texi" |
@c TeX-master: "guile.texi" |
426 |
@c End: |
@c End: |