david hol
david hol

Reputation: 1280

Get number from URL string

So i have this URL

val url = "bla_bla/5555/review"

And i want to get the number 5555. This is what i have try:

val spl = url.split("/")
val number = spl(spl.length - 2)

Any other suggestions ?

Upvotes: 1

Views: 193

Answers (1)

satendra
satendra

Reputation: 66

val url = "bla_bla/5555/review"
val pattern = "[0-9]+".r

To find a single match, you can do

pattern.findFirstMatchIn(url)

For more than one match, you can do

 pattern.findAllMatchIn(url)

Upvotes: 2

Related Questions