LoginSignup
0
0

More than 5 years have passed since last update.

clojure.string/replace の使い方

Last updated at Posted at 2019-01-07

replace には下記のように、第2引数に正規表現、第3引数に関数を渡すことができるが Clojure のドキュメント https://clojuredocs.org/clojure.string/replace には渡す関数についての説明が一切ないので調べる。

(clojure.string/replace string regex function)

以下の通り実行した結果、関数は正規表現にマッチした文字列毎に呼び出され、引数としてマッチした文字列全体 $& と括弧で記憶した部分文字列 $1 $2 .. が Vector で渡される。[$& $1 $2 $3 ...]

(defn r-str [a]
  (println (type a))
  (println a)
  (println (clojure.string/join " : " a))
  (clojure.string/join " " a))
(clojure.string/replace "#1 #12 #123 #1234" #"#(\d)(\d+)" r-str)
clojure.lang.PersistentVector
[#12 1 2]
#12 : 1 : 2
clojure.lang.PersistentVector
[#123 1 23]
#123 : 1 : 23
clojure.lang.PersistentVector
[#1234 1 234]
#1234 : 1 : 234

余談

Clojure のドキュメント(doc 含む)は記述方法に統一感が無いので読んで動作が推測できるようになるのはどれくらいかかるのか検討もつかない。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0