Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can re-seq be made to behave like .split?

Tags:

regex

clojure

Is there some regex that can be passed to re-seq such that it will behave like .split?

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

As you can see the regex [^,] is not quite acceptable because it won't pick up on empty columns in a delimited file. Am I stuck with .split or can re-seq be made to work?

like image 997
Kevin Avatar asked Nov 25 '25 17:11

Kevin


1 Answers

Try

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

I hope that Clojure regexes support lookbehind assertions.

Explanation:

[^,]+       # Either match one or more non-comma characters
|           # or
(?<=,)(?=,) # match the empty string between two commas
like image 158
Tim Pietzcker Avatar answered Nov 27 '25 07:11

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!