Last active
November 15, 2017 09:26
-
-
Save jeroenr/3358869 to your computer and use it in GitHub Desktop.
Scala pattern matching with regex: parsing weird integer strings
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
val MaxInt = """(inf)""".r | |
val NormalInt = """(\d*)""".r | |
def parseInt(integerString:String): Int = { | |
integerString match { | |
case MaxInt(_) => Integer.MAX_VALUE | |
case NormalInt(_) => Integer.valueOf(integerString) | |
case _ => throw new NumberFormatException(String.format("Cannot parse %s as an integer", integerString)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NVM it is built in thanks!