If you need to validate user input, find and replace phrases or words in files, or extract information from documents, then you want to use regular expressions. Regular expressions, also referred to as regex, are the go-to tool for programmers when dealing with any sort of find, find-replace, or data extraction involving strings. I’ve worked with regular expressions for decades, and never found a situation where a match needed to be made, that regular expressions couldn’t come to the rescue.
Clojure regular expressions are based off of Java’s regular expressions API. Clojure uses Pattern and Matcher extensively. However, you don’t need to learn Java to use Clojure’s Regular Expressions. This tutorial teaches the basics of using Clojure’s regex.
Continue reading “Introduction to Clojure Regular Expressions”
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,
(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")
"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)))
would print out, Continue reading “Clojure Regex”