当星号包围子字符串时,我需要用空格替换子字符串的字符:
(def my-line "Lorem ipsum dolor, *consetetur*, invidunt ut *labore et dolore magna aliquyam* erat")
(clojure.string/replace my-line #"\*(.*?)\*" " ")
但是我的代码很笨,设置了一个静态的空格,我需要用空格替换相同数量的字符:
consetetur = 9 spaces
labore et dolore magna aliquyam = 31
答案 0 :(得分:0)
解决方案。
如andy_fingerhut所述,当正则表达式捕获到几组字符时,会创建并传递一个向量:
(clojure.string/replace my-line #"\*(.*?)\*" #(.toUpperCase (%1 1)))
=> "Lorem ipsum dolor sit amet, CONSETETUR, tempor invidunt ut LABORE ET DOLORE MAGNA ALIQUYAM erat, sed diam"
所以就我而言:
(clojure.string/replace my-line #"\*(.*?)\*"
#(clojure.string/join (take (count (% 1)) (repeat " "))))
=> "Lorem ipsum dolor sit amet, , tempor invidunt ut erat, sed diam"