Last active
August 29, 2015 14:21
-
-
Save cy6erGn0m/6dae892d1a0d72f5e4ba to your computer and use it in GitHub Desktop.
Demostrates Power of Kotlin's multi-declarations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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