Skip to content

Instantly share code, notes, and snippets.

@daithiocrualaoich
Created February 7, 2012 15:43
Show Gist options
  • Save daithiocrualaoich/1760314 to your computer and use it in GitHub Desktop.
Save daithiocrualaoich/1760314 to your computer and use it in GitHub Desktop.
URL parameter parsing
val Param = """(.+)=(.+)""".r
def params(parameters: String): Map[String, String] = {
(parameters split "\\?") match {
case Array(path, queryString) =>
val kvs = (queryString split "&").toList collect {
case Param(key, value) => key -> value
}
kvs.toMap
case _ => Map()
}
}
// Tests
assert(params("?q=query&r=result&s=stuff") == Map("q" -> "query", "r" -> "result", "s" -> "stuff"))
assert(params("http://www.gu.com/") == Map())
assert(params("http://www.gu.com?q=query&r=result&s=stuff") == Map("q" -> "query", "r" -> "result", "s" -> "stuff"))
assert(params("?q=query&r=result&s=stuff") == Map("q" -> "query", "r" -> "result", "s" -> "stuff"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment