Created
June 22, 2012 00:23
-
-
Save davidandrzej/2969461 to your computer and use it in GitHub Desktop.
Scala regex unapply magic
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
scala> val myRegex = """Foo=([0-9]+) Bar=([A-Z]+)""".r | |
myRegex: scala.util.matching.Regex = Foo=([0-9]+) Bar=([A-Z]+) | |
scala> "Foo=123 Bar=ABC" match { | |
| case myRegex(foo, bar) => | |
| println("foobar looks like %s-%s".format(foo,bar)) | |
| case _ => println("not a match") | |
| } | |
foobar looks like 123-ABC | |
scala> "LOLCATS4LIFE" match { | |
| case myRegex(foo, bar) => | |
| println("foobar looks like %s-%s".format(foo,bar)) | |
| case _ => println("not a match") | |
| } | |
not a match |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment