Skip to content

Instantly share code, notes, and snippets.

@rbonvall
Created May 8, 2015 21:46
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 rbonvall/b5f99b407af7a2709626 to your computer and use it in GitHub Desktop.
Save rbonvall/b5f99b407af7a2709626 to your computer and use it in GitHub Desktop.
Extractors for numbers in strings
object f {
val floatingPointNumberPattern = """(\d+(?:[.]\d*))""".r
def unapply(s: String): Option[Double] = s match {
case floatingPointNumberPattern(x) ⇒ Some(x.toDouble)
case _ ⇒ None
}
}
object i {
val nonNegativeIntegerPattern = """(\d+)""".r
def unapply(s: String): Option[Int] = s match {
case nonNegativeIntegerPattern(x) ⇒ Some(x.toInt)
case _ ⇒ None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment