相当于重新匹配器和重新分组的 clojurescript
clojurescript equivalent of re-matcher and re-groups
我希望在 clojurescript 中实现 seperate-humps
,但没有 re-matcher
或 re-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"
我写了一个函数来重现 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" } ]
我希望在 clojurescript 中实现 seperate-humps
,但没有 re-matcher
或 re-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"
我写了一个函数来重现 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" } ]