Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic regex argument in re-find function in clojure

Tags:

regex

clojure

I'm using the re-find function in clojure, and have something like this:

(defn some-function []
(re-find #"(?i)blah" "some sentence"))

What I would like is to make the "blah" dynamic, so I substituted a var for blah like this, but it doesn't work:

(defn some-function2 [some-string]
(re-find #(str "(?i)" some-string) "some sentence"))

I'm surprised this doesn't work since LISP is supposed to "treat code like data".

like image 973
animalcroc Avatar asked Mar 05 '14 16:03

animalcroc


1 Answers

Use the function re-pattern. #"" is just a reader macro (aka. syntactic sugar for creating regex)

#(str "(?i)" some-string) is reader macro to create an anonymous functions.

like image 59
DanLebrero Avatar answered Sep 21 '22 20:09

DanLebrero