If you use capturing parenthesis in the regular expression pattern in Python's
re.split() function, it will include the matching groups in the result (Python's documentation).
I need this in my Clojure code and I didn't find an implementation of this, nor a Java method for achieving the same result.
Anyway, here is my code:
(use '[clojure.string :as string :only [blank?]]) (defn re-tokenize [re text] (let [matcher (re-matcher re text)] (defn inner [last-index result] (if (.find matcher) (let [start-index (.start matcher) end-index (.end matcher) match (.group matcher) insert (subs text last-index start-index)] (if (string/blank? insert) (recur end-index (conj result match)) (recur end-index (conj result insert match)))) (conj result (subs text last-index)))) (inner 0 )))
(re-tokenize #"(\W+)" "...words, words...") => ["..." "words" ", " "words" "..." ""]
How could I make this simpler and / or more efficient (maybe also more Clojure-ish)?