How to filter a directory listing with a regular expression in Clojure
clojure, regex
Solution
(defn regex-file-seq
"Lazily filter a directory based on a regex."
[re dir]
(filter #(re-find re (.getPath %)) (file-seq dir)))
Problem
Given a regex and a directory path, how do I get a seq of files whose path matches the regex? i.e., I want ``` (require '[clojure.java.io :as io]) (regex-file-seq #"\.ssh/.*\.pub$" (io/file "/home")) => (#<File /home/x/.ssh/iaf_dsa.pub> #<File ....>) ``` This took me a few minutes to put the pieces together, so I thought I'd post it.