Last active
October 10, 2015 02:18
-
-
Save tachesimazzoca/3617264 to your computer and use it in GitHub Desktop.
Regex examples #scala
This file contains 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
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