给定一些样本数据:
(ns tst.demo.core
(:use demo.core tupelo.core tupelo.test)
(:require
[clojure.string :as str]
[tupelo.string :as ts]
[tupelo.parse :as parse]))
(def data-str "
fred123 1 2 3
fred456 4 5 6
wilma12 1.2
wilma34 3.4
barney1 1
barney2 2
")
然后,您可以为每种类型的数据定义解析函数:
(defn fred-parser
[line]
(let [tokens (str/split line #"\p{Blank}+")
root (first tokens)
details (rest tokens)
parsed-root (re-find #"fred\n*" root)
parsed-params (mapv parse/parse-long details)
result {:root parsed-root :params parsed-params}]
result))
(defn wilma-parser
[line]
(let [tokens (str/split line #"\p{Blank}+")
root (first tokens)
details (rest tokens)
parsed-root (re-find #"wilma\n*" root)
parsed-params (mapv parse/parse-double details)
result {:root parsed-root :params parsed-params}]
result))
我会制作一个从模式到解析函数的映射:
(def pattern->parser
{#"fred\d*" fred-parser
#"wilma\d*" wilma-parser
})
以及为每一行(已清理)数据找到正确解析器的一些函数:
(defn parse-line
[line]
(let [patterns (keys pattern->parser)
patterns-matching (filterv ; keep pattern if matches
(fn [pat]
(ts/contains-match? line pat))
patterns)
num-matches (count patterns-matching)]
(cond
(< 1 num-matches) (throw (ex-info "Too many matching patterns!" {:line line :num-matches num-matches}))
(zero? num-matches) (prn :no-match-found line)
:else (let [parser (get pattern->parser (only patterns-matching))
parsed-line (parser line)]
parsed-line))))
(defn parse-file
[data]
(let
[lines (filterv #(not (str/blank? %)) ; remove blank lines
(mapv str/trim ; remove leading/trailing whitespace
(str/split-lines data))) ; split into lines
parsed-data (mapv parse-line lines)]
parsed-data))
和一个单元测试来展示它的实际效果:
(dotest
(is= (parse-file data-str)
[{:root "fred", :params [1 2 3]}
{:root "fred", :params [4 5 6]}
{:root "wilma", :params [1.2]}
{:root "wilma", :params [3.4]}
nil
nil])
)
请注意,不匹配的行返回 nil。您可能希望针对问题抛出异常,或者至少过滤掉这些nil
值。现在你只得到一个打印的错误消息:
-------------------------------
Clojure 1.10.1 Java 14
-------------------------------
Testing tst.demo.core
:no-match-found "barney1 1"
:no-match-found "barney2 2"
Ran 2 tests containing 1 assertions.
0 failures, 0 errors.
更多文档在这里和这里。