Created
May 8, 2015 21:46
-
-
Save rbonvall/b5f99b407af7a2709626 to your computer and use it in GitHub Desktop.
Extractors for numbers in strings
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
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