Last active
August 29, 2015 14:20
-
-
Save justinhj/7941cbd34dc3f6f5b2e3 to your computer and use it in GitHub Desktop.
Using unapply to match strings that are valid filenames with an extension
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
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