Created
April 3, 2014 16:21
-
-
Save kurogelee/9957644 to your computer and use it in GitHub Desktop.
Clojureで正規表現での検索結果をマップに変換する ref: http://qiita.com/kurogelee/items/5f94c1a520129470516a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user=> (re-find #".\d(.)" "some1234abc") | |
["e12" "2"] | |
user=> (re-find->map #".\d(.)" "some1234abc" :1 :2) | |
{:1 "e12", :2 "2"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user=> (re-find-lines->map ["some1234abc", "A9"] #".\d(.)" [:1 :2] #"A" :a) | |
{:a "A", :1 "e12", :2 "2"} | |
user=> (re-find-lines->map ["some1234abc", "A9"] #".\d(.)" [:1 :2] #"\d" :a) | |
{:a "1", :1 "e12", :2 "2"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user=> (re-seq #".\d(.)" "some1234abc") | |
(["e12" "2"] ["34a" "a"]) | |
user=> (re-seq->map #".\d(.)" "some1234abc" :1 :2) | |
{:1 ("e12" "34a"), :2 ("2" "a")} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user=> (re-seq-lines->map ["some1234abc", "A9"] #".\d(.)" [:1 :2] #"A" :a) | |
{:a ("A"), :1 ("e12" "34a"), :2 ("2" "a")} | |
user=> (re-seq-lines->map ["some1234abc", "A9"] #".\d(.)" [:1 :2] #"\d" :a) | |
{:a ("1" "2" "3" "4" "9"), :1 ("e12" "34a"), :2 ("2" "a")} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn zipseq [& colls] (apply map list colls)) | |
(defn indexed [coll] (zipseq (range) coll)) | |
(defn- count<> [colls f] (->> (map count colls) (apply f))) | |
(defn count< [& colls] (count<> colls <)) | |
(defn re-find-lines->map [lines & re-keys-pairs] | |
{:pre[(even? (count re-keys-pairs))]} | |
(let [rk-list (indexed (partition 2 re-keys-pairs)) | |
m (java.util.LinkedHashMap.)] | |
(doseq [line lines | |
[i [re keys]] rk-list | |
:while (count< m rk-list) | |
:when (not (contains? m i)) | |
:let [v (apply re-find->map re line (wrap-vec keys))] | |
:when v] (.put m i v)) | |
(apply merge (reverse (vals m))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn wrap-vec [x] (if-not (sequential? x) [x] (vec x))) | |
(defn re-find->map [re s & keys] | |
(when-let [v (re-find re s)] | |
(apply hash-map (interleave keys (wrap-vec v))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn re-seq-lines->map [lines & re-keys-pairs] | |
{:pre[(even? (count re-keys-pairs))]} | |
(->> (for [line lines [re keys] (partition 2 re-keys-pairs)] | |
(apply re-seq->map re line (wrap-vec keys))) | |
(apply merge-with concat))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn re-seq->map [re s & keys] | |
(when-let [v (re-seq re s)] | |
(apply hash-map (interleave keys (apply zipseq (map wrap-vec v)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment