2012-01-07 27 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")) 
相關問題