How to get the first number from a string in Scala

1 Answer

0 votes
import scala.util.matching.Regex

object Main extends App {
  val str = "abc87def1009g15hi"

  val re: Regex = "[0-9]+".r

  val result = re.findAllIn(str).toList

  println(result.head)
}

 
   
/*
           
run:
     
87
       
*/

 



answered Sep 7, 2024 by avibootz

Related questions

1 answer 136 views
1 answer 121 views
3 answers 185 views
3 answers 175 views
...