I have a Array of Strings
scala> tokens
res34: Array[String] = Array(The, value, of, your, profile, is, 234.2., You, have, potential, to, gain, 8.3, more.)
>
In Scala there is a syntactic sugar to compose map
and filter
called for comprehension. Below is a version of regex approach based on for
:
val regex = ".*\\d+\\.?\\d+.*"
val nums = for {
str <- tokens if str.matches(regex)
numStr = str.trim.split("\\W+").mkString(".")
} yield numStr.toDouble
It gives the desired output:
nums: Array[Double] = Array(234.2, 8.3)