2012-01-07 24 views

回答

7
(def my-re (java.util.regex.Pattern/compile "/")) ; to turn a string into a regex 
;; or just 
(def my-re #"/") ; if the regex can be a literal 

(clojure.string/split "foo/bar" my-re) 
+0

哇,这是快!非常感谢! – 2012-01-07 12:36:57

+17

(重新模式“/”)将会做得更好,比(java.util.regex.Pattern/compile“/”)更简洁 – NielsK 2012-01-07 13:36:29

+0

很好,非常感谢。 – 2012-01-07 15:33:06

11

您可以使用re-pattern

(def var "/")    ; variable containing a string 
(def my-re (re-pattern var)) ; variable string to regex 

(clojure.string/split "foo/bar" my-re) 

或者,使用一个线程最后一个宏:

(->> "/" 
    (re-pattern) 
    (clojure.string/split "foo/bar")) 
相关问题