7

我正在开发一个 clojure 项目,它可以与任何 java 类互操作,所以我的问题的答案可能是 java 或 clojure。

基本上我需要能够根据给定的分隔符(将超过一个字符)将字符串拆分为组件,但同时保留分隔符。

例如:

splitting "test:test:test" on ":"  => [ "test" ":" "test" ":" "test" ]
splitting "::test::test::" on "::" => [ "::" "test" "::" "test" "::" ]

我来的壁橱使用 clojure's clojure.string/split,但它实际上并没有返回分隔符。第二个最接近的是使用 StringTokenizer,它确实返回分隔符但不接受多字符分隔符。

除了将字符串分解成一系列字符并对其进行奇怪的减少之外,有谁知道任何解决方案?

4

2 回答 2

8

这是一个版本,它构建了一个正则表达式来匹配分隔符前后的间隙,而不是分隔符字符串本身(假设 regex 中没有特殊字符d):

=> (defn split-with-delim [s d]
     (clojure.string/split s (re-pattern (str "(?=" d ")|(?<=" d ")"))))
#'user/split-with-delim
=> (split-with-delim "test:test:test" ":")
["test" ":" "test" ":" "test"]
=> (split-with-delim "::test::test::" "::")
["" "::" "test" "::" "test" "::"]
于 2013-03-08T10:09:28.543 回答
4
(defn split-it [s d]
  (interpose d (str/split s (re-pattern d))))

(split-it "test:test:test" ":")
=> ("test" ":" "test" ":" "test")

(split-it "::test::test::" "::")
=> ("" "::" "test" "::" "test")
于 2013-03-08T04:41:14.000 回答