aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/str_utils
diff options
context:
space:
mode:
authorChouser <chouser@n01se.net>2008-09-12 17:43:33 +0000
committerChouser <chouser@n01se.net>2008-09-12 17:43:33 +0000
commit302cf65ab8c9827cd72c39588285cadd69d5f4b3 (patch)
treeff1ab570861e56138f65d24945f5b2223b0628e9 /src/clojure/contrib/str_utils
parent63b991f43265cd9834be613ffe86e1abe02f72f9 (diff)
Add re-partition to str-utils
Diffstat (limited to 'src/clojure/contrib/str_utils')
-rw-r--r--src/clojure/contrib/str_utils/str_utils.clj21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/clojure/contrib/str_utils/str_utils.clj b/src/clojure/contrib/str_utils/str_utils.clj
index 8fbcd8c1..fb5404e7 100644
--- a/src/clojure/contrib/str_utils/str_utils.clj
+++ b/src/clojure/contrib/str_utils/str_utils.clj
@@ -22,6 +22,27 @@
([#^Pattern pattern string] (seq (. pattern (split string))))
([#^Pattern pattern string limit] (seq (. pattern (split string limit)))))
+(defn re-partition
+ "Splits the string into a lazy sequence of substrings, alternating
+ between substrings that match the patthern and the substrings
+ between the matches. The sequence always starts with the substring
+ before the first match, or an empty string if the beginning of the
+ string matches.
+
+ For example: (re-partition #\"[a-z]+\" \"abc123def\")
+
+ Returns: (\"\" \"abc\" \"123\" \"def\")"
+ [#^Pattern re string]
+ (let [m (re-matcher re string)]
+ ((fn step [prevend]
+ (if (.find m)
+ (lazy-cons (.subSequence string prevend (.start m))
+ (lazy-cons (re-groups m)
+ (step (+ (.start m) (count (.group m))))))
+ (when (< prevend (.length string))
+ (list (.subSequence string prevend (.length string))))))
+ 0)))
+
(defn re-gsub
"Replaces all instances of 'pattern' in 'string' with
'replacement'. Like Ruby's 'String#gsub'."