Clojure:
Searching and replacing text
How to:
In Clojure, we wield the clojure.string/replace
function to search and replace text. Let’s cut to the chase with some code:
(require '[clojure.string :as str])
;; Basic replacement
(str/replace "I like apples" "apples" "oranges")
;; => "I like oranges"
;; Using a regular expression to replace all vowels
(str/replace "Hello, World!" "[AEIOUaeiou]" "*")
;; => "H*ll*, W*rld!"
;; Replacement with a function for dynamic changes
(str/replace "I have 2 apples and 5 bananas"
#"\d+"
(fn [match] (str (inc (Integer/parseInt match)))))
;; => "I have 3 apples and 6 bananas"
Simple as that. Run it, and you’ll see the transformations right there in your REPL.
Deep Dive
Searching and replacing in text isn’t new. It’s age-old in computing. We got it from early editors like sed
in Unix. We’ve come a long way since then.
Clojure, being on the JVM, means you’ve got Java’s regular expression prowess under the hood. Performance-wise, it’s nifty for quick scripts but remember, overuse in large-scale text processing can hurt performance.
As for alternatives, besides clojure.string/replace
, there’s regex-based libraries or even writing your custom function if you’re feeling adventurous. Think about replace-first
if you only need a one-shot change.
Functionally, Clojure’s approach to immutability means each replacement results in a new string. No mutable strings mean fewer bugs and surprises.
See Also
To dive deeper, check out these resources:
- Clojure’s
clojure.string
API documentation - On regular expressions, Java’s Pattern class