我希望捕获的日期格式属于“word / DD / YYYY”模式的排列,其中单词对应于月份,即
(def months ["january" "January" "february" "February" "march" "March" "April" "april" "may" "May" "june" "June" "july" "July" "august" "August" "september" "September" "october" "October" "november" "November" "december" "December"])
因此,上述模式的可能排列将是“DD / word / YYYY”“YYYY / word / DD”和“YYYY / DD / word”
我尝试了一些
的方法(def months-match (clojure.string/join "|" months))
(def months-str (str "(\\s*(" months-match ")"))
(def moster (re-pattern months-str))
(defn foomonths [s]
(map first (re-seq moster s)))
计划在几天内添加正则表达式
|[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
置换正则表达式不是问题。相反,它是将数月的单词制定成正则表达式结构的过程,其中日期和年份以数字表示。
答案 0 :(得分:7)
我看到你的问题是关于正则表达式的,所以如果这个答案不合适,我很抱歉,不过如果我建议采用略有不同的方法,clj-time包括一个可以处理大部分开箱即用的时间格式器:
project.clj:
(defproject hello "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[clj-time "0.6.0"]]
:source-paths ["dev"])
user> (def custom-formatter (formatter "dd/MMMMMMMMM/YYYY"))
#'user/custom-formatter
user> (parse custom-formatter "14/June/2014")
#<DateTime 2014-06-14T00:00:00.000Z>
user> (parse custom-formatter "14/september/2014")
#<DateTime 2014-09-14T00:00:00.000Z>
因此,您可以为每个排列编写一次格式字符串,然后尝试各自直到您获得匹配