Skip to content

Instantly share code, notes, and snippets.

@tachesimazzoca
Last active October 10, 2015 02:18
Show Gist options
  • Save tachesimazzoca/3617264 to your computer and use it in GitHub Desktop.
Save tachesimazzoca/3617264 to your computer and use it in GitHub Desktop.
Regex examples #scala
val urlMatcher: (String => Option[Map[Symbol, String]]) = { str =>
val ptn = """^(https?)://([^/]+)(.*)$""".r
for { ptn(m1, m2, m3) <- ptn findFirstIn str } yield {
Map(
'scheme -> m1,
'host -> m2,
'uri -> m3
)
}
}
Array(
"http://example.net",
"https://example.net/",
"http://example.net/index.html",
"http://example.net/posts/123"
) foreach { str =>
urlMatcher(str) map { matches =>
println("Scheme:%s Host:%s URI:%s".format(
matches('scheme), matches('host), matches('uri))
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment