Skip to content

String replace with regex

String replace with regex patternλ︎

clojure.string/replace takes a string, a pattern and a substring that will replace matching patterns.

(clojure.string/replace "mississippi" #"i.." "obb")

Groups can be referred to in the substring replacement

(clojure.string/replace "mississippi" #"(i)" "$1$1")

Replace with the value of a function applied to the match:

(clojure.string/replace "mississippi" #"(.)i(.)"
     (fn [[_ b a]]
       (str (clojure.string/upper-case b)
            (clojure.string/upper-case a))))

clojure.string/replace-first is a variation where just the first occurrence is replaced.