ClojureScript中re-matcher和re-groups的等效方法

3

我想在ClojureScript中实现separate-humps,但是没有re-matcherre-groups。是否有其他替代方法?

(defn re-sub
  [^String value pattern sub-func]
  (loop [matcher (re-matcher pattern value)
         result []
         last-end 0]
    (if (.find matcher)
      (recur matcher
             (conj result
                   (.substring value last-end (.start matcher))
                   (sub-func (re-groups matcher)))
             (.end matcher))
      (apply str (conj result (.substring value last-end))))))

(defonce +hump-pattern+ #"[a-z0-9][A-Z]")

(defn separate-humps
  [^String value]
  (re-sub value +hump-pattern+ #(string/join " " (seq %))))

(separate-humps "aTaTa")
;;=> "a Ta Ta"
1个回答

3
我编写了一个函数,它能够复制re-matcher的一般功能:
(defn grouper
  "Uses js/RegExp to find matching groups. Note that the JS value 
   returned by `:last-index` is the index of the first char in the 
   input string *after* the current match."
  [re input-str]
  (let [re-src re.source] ; the source string from the regexp arg
    (loop [groups []
           regexp (js/RegExp. re-src "g")] ; 'g' => global search
      (let [res     (.exec regexp input-str)
            res-clj (js->clj res)]
        (if (nil? res)
          groups
          (recur
            (conj groups {:groups res-clj :match (get res-clj 0) 
                          :index res.index :input res.input
                          :last-index regexp.lastIndex})
            regexp))))))

输出结果如下:

(grouper #"[a-z0-9][A-Z]"  "aTaTa")  =>
    [ {:groups ["aT"]  :match "aT"  :index 0  :last-index 2  :input "aTaTa" }
      {:groups ["aT"]  :match "aT"  :index 2  :last-index 4  :input "aTaTa" } ]

(grouper  #"((\d+)-(\d+))" "672-345-456-3212")  =>
    [ {:groups ["672-345"  "672-345"  "672" "345" ]  :match "672-345"   :index 0  :last-index  7  :input "672-345-456-3212" }
      {:groups ["456-3212" "456-3212" "456" "3212"]  :match "456-3212"  :index 8  :last-index 16  :input "672-345-456-3212" } ]
  

您可以在这里找到文档

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接