Shirley Du Shirley Du - 1 year ago 119
Scala Question

Convert string representation of list to list in Scala

If I have a string representation of list

s = '["a", "b", "c"]'
, how do I parse this string to extract the list object? Expected output
l = ["a", "b", "c"]

Answer Source
val str = """["a","b" "c"]"""    // string with quote marks
val getStrs = "\"([^, ]+)\"".r   // regex to isolate quoted strings

Now to pull those quoted strings (without the quote marks) into a List[String].

val lst = (for (m <- getStrs findAllMatchIn str) yield m group 1).toList
// lst: List[String] = List(a, b, c)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download