1

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 ?

3
  • Why not use java.net.URI? Commented Jul 21, 2016 at 16:02
  • i didn't familiar with that, any example ? Commented Jul 21, 2016 at 16:03
  • 2
    Does your code work alright? -> No - ask a question on SO. Yes, but I want to enhance it -> ask at Code Review. Commented Jul 21, 2016 at 16:04

1 Answer 1

2
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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.