2016-05-31 8 views

Antwort

1
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 

Offensichtlich passen Sie \\d{1,3} an Ihre Anforderungen an, wenn die Länge der Zahlen zwischen und einschließlich 1 und 3

entspricht
0

Dies hängt auf der gesamten Struktur des Satzes, aber sollte es tun:

val str = "\n Displaying names 1 - 20 of 273 in total" 

val matches = """of\s+(\d+)\s+in total""".r.findAllMatchIn(str) 

matches.foreach { m => 
    println(m.group(1).toInt) 
}