Leothorn
Leothorn

Reputation: 1345

Scala: Convert a string to string array with and without split given that all special characters except "(" an ")" are allowed

I have an array

val a = "((x1,x2),(y1,y2),(z1,z2))"

I want to parse this into a scala array

val arr = Array(("x1","x2"),("y1","y2"),("z1","z2"))

Is there a way of directly doing this with an expr() equivalent ? If not how would one do this using split

Note : x1 x2 x3 etc are strings and can contain special characters so key would be to use () delimiters to parse data -

Code I munged from Dici and Bogdan Vakulenko

val x2 = a.getString(1).trim.split("[\()]").grouped(2).map(x=>x(0).trim).toArray

val x3 = x2.drop(1) // first grouping is always null dont know why

var jmap = new java.util.HashMap[String, String]()

for (i<-x3)
{
 val index = i.lastIndexOf(",")
 val fv = i.slice(0,index)
 val lv = i.substring(index+1).trim
 jmap.put(fv,lv)
}

This is still suceptible to "," in the second string -

Upvotes: 0

Views: 1956

Answers (2)

Bogdan Vakulenko
Bogdan Vakulenko

Reputation: 3390

val a = "((x1,x2),(y1,y2),(z1,z2))"

a.replaceAll("[\\(\\) ]","")
 .split(",")
 .sliding(2)
 .map(x=>(x(0),x(1)))
 .toArray

Upvotes: 1

Dici
Dici

Reputation: 25950

Actually, I think regex are the most convenient way to solve this.

val a = "((x1,x2),(y1,y2),(z1,z2))"
val regex = "(\\((\\w+),(\\w+)\\))".r
println(
  regex.findAllMatchIn(a)
       .map(matcher => (matcher.group(2), matcher.group(3)))
       .toList
)

Note that I made some assumptions about the format:

  • no whitespaces in the string (the regex could easily be updated to fix this if needed)
  • always tuples of two elements, never more
  • empty string not valid as a tuple element
  • only alphanumeric characters allowed (this also would be easy to fix)

Upvotes: 2

Related Questions