0

i have a string of number with dots seperating them, max 3 numbers, for example:

option1 - "1" (just one digit)

option2 - "1.2"

option3 - "1.2.4"

I want to split those numbers and have them stored in separate values, this was my solution:

val numbersRegex = """([^\.]+)\.?(\d)*\.?(\d)?""".r

def splitNumber(number: String): (Option[String], Option[String], Option[String]) = {
  val numbersRegex(first, second, third) = number
  (Option(first), Option(second), Option(third))
}

this worked good, but i found a problem for when I have number with more than one digit, like "1.14.5"

in this case I will get:

(Some(1),Some(4),Some(5))

which what I expect is

(Some(1),Some(14),Some(5))

someone know how should I fix it?

John Deer
  • 119
  • 11

1 Answers1

1

Using split function is much more concise.

scala> "1.14.5".split('.')
res2: Array[String] = Array(1, 14, 5)
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40