Skip to content

Instantly share code, notes, and snippets.

@jeroenr
Last active November 15, 2017 09:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenr/3358869 to your computer and use it in GitHub Desktop.
Save jeroenr/3358869 to your computer and use it in GitHub Desktop.
Scala pattern matching with regex: parsing weird integer strings
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))
}
}
@nmccready
Copy link

NVM it is built in thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment