scala> import scala.util.matching.Regex import scala.util.matching.Regex scala> val matcher = new Regex("\\d{1,3}") matcher: scala.util.matching.Regex = \d{1,3} scala> val string = "\n Displaying names 1 - 20 of 273 in total" string: String = " Displaying names 1 - 20 of 273 in total" scala> matcher.findAllMatchIn(string).toList.reverse.head.toString.toInt res0: Int = 273
Obviously adjust \\d{1,3} according to your requirements when the length of the matching numbers is between 1 and 3 and includes
source share