In Scala I have a string of the form
val str = "[ab][bc][cd][dx][42]"
What is the most efficient way in Scala, regular expression utilizing or otherwise, to split that string into a Seq[String] that would be of the form:
("ab","bc","cd","dx","42")
Thank you.
You can try this:
val str = "[ab][bc][cd][dx][42]"
val res = str.drop(1).sliding(2, 4).toList
println(res) // List(ab, bc, cd, dx, 42)
val str2 = "[ab]"
val res2 = str2.drop(1).sliding(2, 4).toList
println(res2) // List(ab)
val str3 = ""
val res3 = str3.drop(1).sliding(2, 4).toList
println(res3) // List()
you can try:
str.split("(\\]\\[)|(\\[)|(\\])").filterNot(_.isEmpty)
Mainly using groups (][, ], [) for delimiter and trimming the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With