Back
replace (clj)
(source)function
(replace s match replacement)
Replaces all instance of match with replacement in s.
match/replacement can be:
string / string
char / char
pattern / (string or function of match).
See also replace-first.
The replacement is literal (i.e. none of its characters are treated
specially) for all cases above except pattern / string.
For pattern / string, $1, $2, etc. in the replacement string are
substituted with the string that matched the corresponding
parenthesized group in the pattern. If you wish your replacement
string r to be used literally, use (re-quote-replacement r) as the
replacement argument. See also documentation for
java.util.regex.Matcher's appendReplacement method.
Example:
(clojure.string/replace "Almost Pig Latin" #"\b(\w)(\w+)\b" "$2$1ay")
-> "lmostAay igPay atinLay"
Examples
clojure
(ns clojure.test-clojure.transducers
(:require [clojure.string :as s]
[clojure.test :refer :all]
[clojure.test.check :as chk]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]
[clojure.test.check.clojure-test :as ctest]))
(def gen-take (fbind (literal gen/s-pos-int) take))
(def gen-drop (fbind (literal gen/pos-int) drop))
(def gen-drop-while (fbind gen-predfn drop-while))
(def gen-map (fbind gen-mapfn map))
(def gen-mapcat (fbind gen-mapcatfn mapcat))
(def gen-filter (fbind gen-predfn filter))
(def gen-remove (fbind gen-predfn remove))
(def gen-keep (fbind gen-predfn keep))
(def gen-partition-all (fbind (literal gen/s-pos-int) partition-all))
(def gen-partition-by (fbind gen-predfn partition-by))
(def gen-take-while (fbind gen-predfn take-while))
(def gen-take-nth (fbind (literal gen/s-pos-int) take-nth))
(def gen-keep-indexed (fbind gen-indexedfn keep-indexed))
(def gen-map-indexed (fbind gen-indexedfn map-indexed))
(def gen-replace (fbind (literal (gen/return (hash-map (range 100) (range 1 100)))) replace))
(def gen-distinct (gen/return {:desc 'distinct :seq (partial distinct) :xf (distinct)}))
(def gen-dedupe (gen/return {:desc 'dedupe :seq (partial dedupe) :xf (dedupe)}))
(def gen-interpose (fbind (literal gen/s-pos-int) interpose))
clojure
(ns clojure.test-clojure.string
(:require [clojure.string :as s])
(:use clojure.test))
(deftest t-replace
(is (= "faabar" (s/replace "foobar" \o \a)))
(is (= "foobar" (s/replace "foobar" \z \a)))
(is (= "barbarbar" (s/replace "foobarfoo" "foo" "bar")))
(is (= "foobarfoo" (s/replace "foobarfoo" "baz" "bar")))
(is (= "f$$d" (s/replace "food" "o" "$")))
(is (= "f\\\\d" (s/replace "food" "o" "\\")))
(is (= "barbarbar" (s/replace "foobarfoo" #"foo" "bar")))
(is (= "foobarfoo" (s/replace "foobarfoo" #"baz" "bar")))
(is (= "f$$d" (s/replace "food" #"o" (s/re-quote-replacement "$"))))
(is (= "f\\\\d" (s/replace "food" #"o" (s/re-quote-replacement "\\"))))
(is (= "FOObarFOO" (s/replace "foobarfoo" #"foo" s/upper-case)))
(is (= "foobarfoo" (s/replace "foobarfoo" #"baz" s/upper-case)))
(is (= "OObarOO" (s/replace "foobarfoo" #"f(o+)" (fn [[m g1]] (s/upper-case g1)))))
(is (= "baz\\bang\\" (s/replace "bazslashbangslash" #"slash" (constantly "\\")))))
(deftest t-replace-first
(is (= "faobar" (s/replace-first "foobar" \o \a)))
(is (= "foobar" (s/replace-first "foobar" \z \a)))
(is (= "z.ology" (s/replace-first "zoology" \o \.)))
(is (= "barbarfoo" (s/replace-first "foobarfoo" "foo" "bar")))
(is (= "foobarfoo" (s/replace-first "foobarfoo" "baz" "bar")))
(is (= "f$od" (s/replace-first "food" "o" "$")))
(is (= "f\\od" (s/replace-first "food" "o" "\\")))
(is (= "barbarfoo" (s/replace-first "foobarfoo" #"foo" "bar")))
(is (= "foobarfoo" (s/replace-first "foobarfoo" #"baz" "bar")))
(is (= "f$od" (s/replace-first "food" #"o" (s/re-quote-replacement "$"))))
(is (= "f\\od" (s/replace-first "food" #"o" (s/re-quote-replacement "\\"))))
(is (= "FOObarfoo" (s/replace-first "foobarfoo" #"foo" s/upper-case)))
(is (= "foobarfoo" (s/replace-first "foobarfoo" #"baz" s/upper-case)))
(is (= "OObarfoo" (s/replace-first "foobarfoo" #"f(o+)" (fn [[m g1]] (s/upper-case g1)))))
(is (= "baz\\bangslash" (s/replace-first "bazslashbangslash" #"slash" (constantly "\\")))))
(deftest nil-handling
(are [f args] (thrown? NullPointerException (apply f args))
s/reverse [nil]
s/replace [nil #"foo" "bar"]
s/replace-first [nil #"foo" "bar"]
s/re-quote-replacement [nil]
s/capitalize [nil]
s/upper-case [nil]
s/lower-case [nil]
s/split [nil #"-"]
s/split [nil #"-" 1]
s/trim [nil]
s/triml [nil]
s/trimr [nil]
s/trim-newline [nil]))
(deftest char-sequence-handling
(are [result f args] (let [[^CharSequence s & more] args]
(= result (apply f (StringBuffer. s) more)))
"paz" s/reverse ["zap"]
"foo:bar" s/replace ["foo-bar" \- \:]
"ABC" s/replace ["abc" #"\w" s/upper-case]
"faa" s/replace ["foo" #"o" (StringBuffer. "a")]
"baz::quux" s/replace-first ["baz--quux" #"--" "::"]
"baz::quux" s/replace-first ["baz--quux" (StringBuffer. "--") (StringBuffer. "::")]
"zim-zam" s/replace-first ["zim zam" #" " (StringBuffer. "-")]
"\\\\ \\$" s/re-quote-replacement ["\\ $"]
"Pow" s/capitalize ["POW"]
"BOOM" s/upper-case ["boom"]
"whimper" s/lower-case ["whimPER"]
["foo" "bar"] s/split ["foo-bar" #"-"]
"calvino" s/trim [" calvino "]
"calvino " s/triml [" calvino "]
" calvino" s/trimr [" calvino "]
"the end" s/trim-newline ["the end\r\n\r\r\n"]
true s/blank? [" "]
["a" "b"] s/split-lines ["a\nb"]
"fa la la" s/escape ["fo lo lo" {\o \a}]))
clojure
(ns clojure.test-clojure.rt
(:require [clojure.string :as string]
clojure.set)
(:use clojure.test clojure.test-helper))
(deftest ns-intern-policies
(testing "you can replace a core name, with warning"
(let [ns (temp-ns)
replacement (gensym)
e1 (with-err-string-writer (intern ns 'prefers replacement))]
(is (string/starts-with? e1 "WARNING"))
(is (= replacement @('prefers (ns-publics ns))))))
(testing "you can replace a defined alias"
(let [ns (temp-ns)
s (gensym)
v1 (intern ns 'foo s)
v2 (intern ns 'bar s)
e1 (with-err-string-writer (.refer ns 'flatten v1))
e2 (with-err-string-writer (.refer ns 'flatten v2))]
(is (string/starts-with? e1 "WARNING"))
(is (string/starts-with? e2 "WARNING"))
(is (= v2 (ns-resolve ns 'flatten)))))
(testing "you cannot replace an interned var"
(let [ns1 (temp-ns)
ns2 (temp-ns)
v1 (intern ns1 'foo 1)
v2 (intern ns2 'foo 2)
e1 (with-err-string-writer (.refer ns1 'foo v2))]
(is (string/starts-with? e1 "REJECTED"))
(is (= v1 (ns-resolve ns1 'foo))))))
logseq/logseq
(ns frontend.components.lazy-editor
(:require [clojure.string :as string]
[rum.core :as rum]
[shadow.lazy :as lazy]
[frontend.ui :as ui]
[frontend.config :as config]
[frontend.state :as state]
[frontend.handler.plugin :refer [hook-extensions-enhancer-by-type]]
[promesa.core :as p]))
(rum/defc editor <
rum/reactive
{:will-mount
(fn [state]
(lazy/load lazy-editor
(fn []
(if-not @loaded?
(p/finally
(p/all (when-let [enhancers (and config/lsp-enabled?
(seq (hook-extensions-enhancer-by-type :codemirror)))]
(for [{f :enhancer} enhancers]
(when (fn? f) (f (. js/window -CodeMirror))))))
(fn []
(-> (p/delay 200)
(p/then #(reset! loaded? true)))))
(reset! loaded? true))))
state)}
[config id attr code options]
(let [loaded? (rum/react loaded?)
theme (state/sub :ui/theme)
code (or code "")
code (string/replace-first code #"\n$" "")] ;; See-also: #3410
(if loaded?
(@lazy-editor config id attr code theme options)
(ui/loading "CodeMirror"))))
clj-kondo/clj-kondo
(ns changelog
(:require [clojure.string :as str]))
(let [changelog (slurp "CHANGELOG.md")
replaced (str/replace changelog
#" #(\d+)"
(fn [[_ issue after]]
(format " [#%s](https://github.com/clj-kondo/clj-kondo/issues/%s)%s"
issue issue (str after))))
replaced (str/replace replaced
#"@([a-zA-Z0-9-_]+)([, \.)])"
(fn [[_ name after]]
(format "[@%s](https://github.com/%s)%s"
name name after)))]
(spit "CHANGELOG.md" replaced))
babashka/sci
(ns changelog
(:require [clojure.string :as str]))
(let [changelog (slurp "CHANGELOG.md")
replaced (str/replace changelog
#" #(\d+)"
(fn [[_ issue]]
(format " [#%s](https://github.com/babashka/sci/issues/%s)"
issue issue)))
replaced (str/replace replaced
#"@(\w+)([, .\)]|$)"
(fn [[_ name after]]
(format "[@%s](https://github.com/%s)%s"
name name after)))]
(spit "CHANGELOG.md" replaced))
clojupyter/clojupyter
(ns clojupyter.install.local-specs
(:require [clojupyter.install.filemap :as fm]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[io.simplect.compose :refer [p]]))
(def IDENT-CHAR-REGEX-STR "[\\w\\d-_\\.=]")
(def IDENT-CHAR-REGEX (re-pattern IDENT-CHAR-REGEX-STR))
(def IDENT-REGEX (re-pattern (str "^" IDENT-CHAR-REGEX-STR "+$")))
(def DEFAULT-TARGET-JARNAME "clojupyter-standalone.jar")
(def KERNEL-JSON "kernel.json")
(def CONDA-JARNAME-RE (re-pattern (str (str/replace DEFAULT-TARGET-JARNAME "." "\\.") "$")))
(def LOGO-ASSET "clojupyter/assets/logo-64x64.png")
(def SCRIPT-ASSETS ["clojupyter/assets/conda-build/build.sh"
"clojupyter/assets/conda-build/post-link.sh"
"clojupyter/assets/conda-build/pre-unlink.sh"
"clojupyter/assets/conda-build/bld.bat"
"clojupyter/assets/conda-build/post-link.bat"
"clojupyter/assets/conda-build/pre-unlink.bat"])
plumatic/dommy
(ns dommy.core
(:require
[clojure.string :as str]
[dommy.utils :as utils]))
(defmacro by-id
"Returns the DOM node with id in document if it exists, otherwise nil.
`id` can be a string or keyword. Expands to `document.getElementById`."
[id]
(let [id (-> id utils/as-str (str/replace #"#" ""))]
`(js/document.getElementById ~id)))
(defmacro by-class
"Returns a sequence of DOM nodes selected by `class`. `class` can be
a string or keyword and should not include the . selector prefix.
Expands to `base.getElementsByClassName`. If `base` node is given,
selection is limited to its descendant nodes."
([class] `(by-class js/document ~class))
([base class]
(let [class (-> class utils/as-str (str/replace "." ""))]
`(dommy.utils/->Array
(.getElementsByClassName ~base ~class)))))