Clojure Regex Patterns

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

Entire books have been written on the regular expression patterns. Regular expressions were designed originally by Larry Wall, the creator of the PERL programming language. He has claimed that he was just being lazy, because he didn’t want write the program to match some text, so he invented an entire new language to solve the problem instead. What he really did was revolutionize finding and replacing text in the entire realm of computer programming and word processing.

Clojure, like most languages worth learning, supports a full suite of regex patterns. In my previous tutorial, I explained the basics of finding words in a string using regular expressions and the Clojure language. However, regex doesn’t show its real power until you don’t have much more than an idea what you are looking for. That’s when regex shines its brightest. Continue reading “Clojure Regex Patterns”

Introduction to Clojure Regular Expressions

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”

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”