Skip to content

Instantly share code, notes, and snippets.

@davidandrzej
Created June 22, 2012 00:23
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 davidandrzej/2969461 to your computer and use it in GitHub Desktop.
Save davidandrzej/2969461 to your computer and use it in GitHub Desktop.
Scala regex unapply magic
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