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

replace-re

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+"  "_")
"__bc"

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.

Example

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

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.

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