Created
December 22, 2015 10:47
-
-
Save cljforge/855b95abbb7a289144d7 to your computer and use it in GitHub Desktop.
Clojure code to escape regExp characters from string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def regex-char-esc-smap | |
(let [esc-chars "()&^%$#!?*."] | |
(zipmap esc-chars | |
(map #(str "\\" %) esc-chars)))) | |
(defn str-to-pattern | |
[string] | |
(->> string | |
(replace regex-char-esc-smap) | |
(reduce str) | |
re-pattern)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn’t work if you already have a backslash in your string: