Created
November 17, 2016 16:32
-
-
Save laymain/8429745ecf0e43276a7868c8be17e57b to your computer and use it in GitHub Desktop.
Scala RegEx pattern matching
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 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