Skip to content

Instantly share code, notes, and snippets.

@laymain
Created November 17, 2016 16:32
Show Gist options
  • Save laymain/8429745ecf0e43276a7868c8be17e57b to your computer and use it in GitHub Desktop.
Save laymain/8429745ecf0e43276a7868c8be17e57b to your computer and use it in GitHub Desktop.
Scala RegEx pattern matching
object HelloWorld {
def main(args: Array[String]) {
val regex = """([^\s]+)\s+([^\s]+)""".r
Array("word1 word2", "word3 word4").map({
case regex(first, second) =>
Console.out.println(s"Found words : $first $second")
case other =>
Console.err.println(s"Unexpected line: $other")
})
}
}
/*
Found words : word1 word2
Found words : word3 word4
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment