【发布时间】:2023-03-05 17:29:01
【问题描述】:
我知道在clojure.string 中有一个split 函数,它返回字符串中不包括给定模式的部分序列。
(require '[clojure.string :as str-utils])
(str-utils/split "Yes, hello, this is dog yes hello it is me" #"hello")
;; -> ["Yes, " ", this is dog yes " " it is me"]
但是,我正在尝试找到一个函数,该函数将令牌作为元素保留在返回的向量中。所以它会像
(split-around "Yes, hello, this is dog yes hello it is me" #"hello")
;; -> ["Yes, " "hello" ", this is dog yes " "hello" " it is me"]
在任何包含的库中是否有执行此操作的函数?任何在外部库中?我一直在尝试自己编写它,但一直无法弄清楚。
【问题讨论】:
-
返回的数组中的任意两个相邻项是否都隐含了缺少的单词?你试图做什么?
-
好点!这是最好的答案。
-
@Shlomi 是的,它是隐式的,但我需要在返回的 vec 中拆分字符串。在这种情况下,因为被拆分的正则表达式只是一个单词,是的。但是说正则表达式是
\[\[.*?\]\]。在这种情况下,很有可能会有[[hello]]和[[yes]]这样的东西,我需要知道匹配的文本是什么以及它在字符串中的位置。