Skip to content

Instantly share code, notes, and snippets.

@justinhj
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinhj/7941cbd34dc3f6f5b2e3 to your computer and use it in GitHub Desktop.
Save justinhj/7941cbd34dc3f6f5b2e3 to your computer and use it in GitHub Desktop.
Using unapply to match strings that are valid filenames with an extension
object FileExt {
def unapply(s: String): Option[String] = {
val parts: Array[String] = s.split('.')
if(parts.length < 2) None
else Some(parts.last)
}
}
List("some/file.jpg", "file.txt", "none", "none.", "").map(s => s match {
case FileExt(s) => s
case _ => ""
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment