Skip to content

Instantly share code, notes, and snippets.

@cy6erGn0m
Last active August 29, 2015 14:21
Show Gist options
  • Save cy6erGn0m/6dae892d1a0d72f5e4ba to your computer and use it in GitHub Desktop.
Save cy6erGn0m/6dae892d1a0d72f5e4ba to your computer and use it in GitHub Desktop.
Demostrates Power of Kotlin's multi-declarations
import java.util.*
import java.util.regex.Pattern
fun main(args: Array<String>) {
x("http://localhost:9090")
x("http://localhost")
x("http://localhost/path/to/it")
x("http://localhost:9091/path/to/it")
x("http://localhost:9091/?p=1")
}
fun x(url: String) {
val (proto, host, port, path, query) =
"^([a-z]+)://([^:/]+)(?::([0-9]+))?(?:(/[^?]*)(\\?.*)?)?$"
.toRegex()
.match(url)
?.groups
?.map { it?.value }
?.drop(1) ?: Collections.nCopies(5, null)
println(url)
println("proto = ${proto}")
println("host = ${host}")
println("port = ${port}")
println("path = ${path}")
println("query = ${query}")
println()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment