Last active
October 10, 2015 02:17
-
-
Save tachesimazzoca/3616882 to your computer and use it in GitHub Desktop.
Parse a time string #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 timeParser: (String => (String, String)) = { str => | |
val ptn = """^(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9])$""".r | |
ptn findFirstIn str match { | |
case Some(ptn(h, m)) => ("%02d".format(h.toInt), "%02d".format(m.toInt)) | |
case None => ("--", "--") | |
} | |
} | |
Array("01:23", "1:23", "12:34", "23:60", "25:00") foreach { str => | |
val (hour, min) = timeParser(str) | |
println("%s:%s".format(hour, min)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment