diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-07-27 06:35:41 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-07-27 06:35:41 -0700 |
commit | 02bbca53efa5c2da8d671bd46e7e410ce533c846 (patch) | |
tree | 668459874a2428914f0dcaf4fd0bc87e00daf057 | |
parent | c04217e25e9b5aeb04ba765a66c9b1e2cc011533 (diff) | |
download | txr-02bbca53efa5c2da8d671bd46e7e410ce533c846.tar.gz txr-02bbca53efa5c2da8d671bd46e7e410ce533c846.tar.bz2 txr-02bbca53efa5c2da8d671bd46e7e410ce533c846.zip |
regsub: allow string in place of regex.
* regex.c (regsub): Use search_str if regex is a string.
* txr.1: Documented.
-rw-r--r-- | regex.c | 13 | ||||
-rw-r--r-- | txr.1 | 15 |
2 files changed, 24 insertions, 4 deletions
@@ -2898,9 +2898,20 @@ val regsub(val regex, val repl, val str) } else { val pos = zero; val out = mkustring(zero); + val slen = if2(stringp(regex), length(regex)); do { - cons_bind (find, len, search_regex(str, regex, pos, nil)); + val find, len; + + if (slen) { + len = slen; + find = search_str(str, regex, pos, nil); + } else { + cons_bind (a, d, search_regex(str, regex, pos, nil)); + find = a; + len = d; + } + if (!find) { if (pos == zero) return str; @@ -53100,7 +53100,9 @@ and .coNP Function @ regsub .synb -.mets (regsub >> { regex | << function } < replacement << string ) +.mets (regsub < regex < replacement << string ) +.mets (regsub < substring < replacement << string ) +.mets (regsub < function < replacement << string ) .syne .desc The @@ -53109,10 +53111,14 @@ function operates in two modes, depending on whether the first argument is a regular expression, or function. -If the first argument is a regular expression it searches +If the first argument is a regular expression or string, then +.code regsub +searches .meta string for multiple occurrences of non-overlapping matches for that -.metn regex . +.meta regex +or +.metn substring . A new string is constructed similar to .meta string @@ -53156,6 +53162,9 @@ which indicates that no replacement is to take place. ;; Replace Hello with Goodbye: (regsub #/Hello/ "Goodbye" "Hello world!") -> "Goodbye world!" + ;; Same, as a simple substring match, rather than regex: + (regsub "Hello" "Goodbye" "Hello world!") -> "Goodbye world!" + ;; Left-anchored replacement with r^ function: (regsub (fr^ #/H/) "J" "Hello, hello!") -> "Jello, hello!" .brev |