How to split digits and letters in scala?

regex, scala

Solution

Regex to the rescue!

val xs = List("X45C", "5K")
val ys = xs map {x => """\d+|\D+""".r.findAllIn(x).toList}

println(ys)
  /* List(List(X, 45, C), List(5, K)) */

If `\D` is the right choice depends on the actual input, consult the pattern docs for further information.

Problem

Waht would be an idiomatic way to split strings that may contain any combination of digits and letters into groups of digits and letters but keeping the order). ex: ``` X45C -> X-45-C 5K -> 5-K ``` How would be an elegant way to implement that?

Original source