fork(1) download
  1. (defn re-quote [s]
  2. (let [special (set ".?*+^$[]\\(){}|")
  3. escfn #(if (special %) (str \\ %) %)]
  4. (apply str (map escfn s))))
  5.  
  6.  
  7. (let [string "ab(c\\Sdef[A-Z]gh)i|kl^m"
  8. quoted (re-quote string)
  9. matches (re-matches (re-pattern quoted) string)]
  10. (println "string:" string)
  11. (println "quoted:" quoted)
  12. (println "mathes:" matches))
Success #stdin #stdout 1.18s 220224KB
stdin
Standard input is empty
stdout
string: ab(c\Sdef[A-Z]gh)i|kl^m
quoted: ab\(c\\Sdef\[A-Z\]gh\)i\|kl\^m
mathes: ab(c\Sdef[A-Z]gh)i|kl^m