FunctionPackage: exclToCDocOverviewCGDocRelNotesFAQIndexPermutedIndex
Allegro CL version 9.0
Significant update since 9.0 release.
8.2 version


Arguments: string regexp substitution &key count (start 0) end case-fold single-line multiple-lines ignore-whitespace

A patch, released in July, 2013, fixed a bug in replace-re having to do with BOS (Beginning of String) and EOS markers. The pre-patch behavior was:

cl-user(133): (replace-re "abc abc bc" "^abc\\s+"  "_")

Note both occurances of 'abc' were replaced by '_'. That is incorrect. The ^ in the regexp argument indicates that 'abc' should be replaced if it is at the beginning of the string (or at the beginning of a line within the string if the multiple-lines argument is true). After applying the patch, the behavior is corrected to:

cl-user(151): (replace-re "abc abc bc" "^abc\\s+"  "_")
"_abc bc"

;; Similarly for EOS:
;; Before patch:
cl-user(101): (replace-re "abc def " "def$" "_" :end 7)
"abc _ "
;; After Patch
cl-user(162): (replace-re "abc def " "def$" "_" :end 7)
"abc def "

This function replaces substrings in string that matches regexp with substitution, returning the new string. It always returns a new string, even if no substitutions are made.

regexp can be a string that specifies a regular expression, or an already-compiled (by compile-re) regular expression. regexp should match a non-zero length string, or an error is signaled.

substitution can be a string, or a function that takes one argument, a list of match substrings returned by the regexp matcher. The function must return a string, which is then used as a substitution string.

The keyword argument count limits the maximum number of substitutions. If it is nil, all occurrences of regexp in string are replaced.

The keyword arguments start and end limit the region in string where matching occurs.

Other keyword arguments are passed to compile-re to compile regexp.

The symbol naming this operator is also exported from the regexp package.


cl-user(14): (replace-re ":f" "(\\W)(\\w)" "\\1 \\2")
": f"
cl-user(15): (replace-re "00a1b23c99" "\\d+" "#")
cl-user(16): (replace-re "12345" "\\d(\\d+)(\\d)" "#\\1.\\2")
cl-user(17): (replace-re "12345" "\\d(?<middle>\\d+)(?<tail>\\d)"
cl-user(18): (replace-re "a12345b67890c" "\\d(?<middle>\\d+)(?<tail>\\d)"
cl-user(19): (replace-re "0123456789" "\\d+"
                          (lambda (matches)
                            (format nil "(~a)" (length (car matches)))))
cl-user(20): (replace-re "a12345b678c90" "\\d+"
                          (lambda (matches)
                            (format nil "(~a)" (length (car matches)))))
cl-user(21): (replace-re "a12345b678c90" "([a-z])(\\d+)"
                          (lambda (matches)
                            (format nil "~a(~a)"
                                    (string-upcase (cadr matches))
                                    (length (caddr matches)))))

See The new regexp2 module in regexp.htm for further information on this function and the regexp2 module.

Copyright (c) 1998-2019, Franz Inc. Oakland, CA., USA. All rights reserved.
This page was not revised from the 8.2 page.
Created 2012.5.30.

Allegro CL version 9.0
Significant update since 9.0 release.
8.2 version