The url interpolation is mainly useful in pattern matching to match and extract
dynamic parameters in URL templates. You can use it to build URL string too, but in
this case it acts as the standard s interpolation.
val userProfile = url"/users/$userId"
userProfile match {
case url"/users/$userId"=> println("user id" -> userId)
case url"/images/$image..."=> println("image path" -> image)
case url"/search?keyword=$keyword&sort=$sort"=> println("keyword" -> keyword)
case _ => println("wrong url")
}
The matcher allow to extract dynamic parts from both the URL path and queryString. For the
path only a fragment or a fragment part can be extracted (no / will be matched):
url"/users/$id/items" = "/users/12/items"// will match with id="12"
url"/users/id-$id/items" = "/users/id-12/items"// will match with id="12"
If you want to capture several fragments, you can use the ... syntax:
url"/files/$file..." = "/files/images/lol.png"// will match with file="images/lol.png"
You can also match and extract from the queryString. The parameter order is not important and
parameters not specified in the pattern will be ignored:
url"/?page=$page&sort=$sort" = "/?sort=asc&page=3&offset=2"// will match with page="3", sort="asc"
url"/?section=home" = "/?section=home&update=now"// will match
url"/?section=home&update=$date" = "/?section=contact&update=now"// won't match
The
url
interpolation is mainly useful in pattern matching to match and extract dynamic parameters in URL templates. You can use it to build URL string too, but in this case it acts as the standards
interpolation.The matcher allow to extract dynamic parts from both the URL path and queryString. For the path only a fragment or a fragment part can be extracted (no
/
will be matched):If you want to capture several fragments, you can use the
...
syntax:You can also match and extract from the queryString. The parameter order is not important and parameters not specified in the pattern will be ignored: