Clojure Regex

Note: If you’re new to regex, check out my Clojure regex introduction.

Clojure programming supports regular expressions, often just called regex. Underneath Clojure’s hood, Clojure is using Java’s java.util.regex package, so it isn’t surprising to see Clojure’s regular expressions (regex) has similarities to Java’s regular expressions.

Now of course, you could write regular expression code directly using Java from Clojure with Java-Interop. For example,

(def some-quote 
  (str "It was the best of times. "
  "It was the worst of times. It was Friday "
  "night and it was late."))

(def day-pattern "\\w*day")

(defn java-interop-regex
  "just doing Clojure regex with Java APIs"
  []
  (let [pat (java.util.regex.Pattern/compile day-pattern) 
        mat (.matcher pat some-quote)
        day-found (.find mat)]
    (println "Is there a day? ... " day-found)))


(java-interop-regex)

would print out, Continue reading “Clojure Regex”