可以重新设置为像.split?

时间:2012-06-20 17:02:05

标签: regex clojure

是否有一些可以传递给re-seq的正则表达式,使其表现得像.split

user=> (seq (.split "a,b,c,,e" ","))
("a" "b" "c" "" "e")
user=> (re-seq #"[^,]" "a,b,c,,e")
("a" "b" "c" "e")
user=>

正如您所看到的,正则表达式[^,]不可接受,因为它不会在分隔文件中的空列上获取。我是坚持使用.split还是re-seq可以工作?

1 个答案:

答案 0 :(得分:6)

尝试

(re-seq #"[^,]+|(?<=,)(?=,)" "a,b,c,,e")

我希望Clojure正则表达式能够支持lookbehind断言。

<强>解释

[^,]+       # Either match one or more non-comma characters
|           # or
(?<=,)(?=,) # match the empty string between two commas