Basti's Scratchpad on the Internet

Regular Expressions in Clojure

Regular expression is a formal language that will allow you to find chunks of text that matches the patterns you specify. Following are a bunch of code examples which I've put together as a mini-reference for my own use.

In clojure you can define a regular expression using,

#"<patter>"

syntax.

To search for match in a string, you can use re-find, it will return either the match or a vector of matches if you have groups.

(re-find #"quick" "The quick brown fox jumps over the lazy dog")
;;"quick"

(re-find #"(f(oo bar))" "foo bar")
;;["foo bar" "foo bar" "oo bar"]

Like other data structures you can threat regex's as sequences too, re-seq will return a lazy sequence of matches.

(re-seq #"h" "The quick brown fox jumps over the lazy dog")
;;("h" "h")

If you are coming from java world one thing that will confuse you is that at first there seems to be no way to specify pattern flags such as,

Instead of those flags you can use embedded flags.

Other posts
comments powered by Disqus