Created
January 11, 2013 16:46
-
-
Save mikemckibben/4512173 to your computer and use it in GitHub Desktop.
Example of regex extractor with pattern matching in scala.
This file contains hidden or 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 Parser { | |
val Result = """mac: (\S+) sites: \[([^]]+)\]""".r | |
def parseLine(line: String): Option[(String,Seq[String])] = line match { | |
case Result(mac,sites) => Some((mac, sites.split(""",\s*""").toSeq)) | |
case _ => None | |
} | |
} | |
val input = io.Source.fromFile("input.txt") | |
val lines = input.getLines() | |
for (opt <- lines.map(Parser.parseLine);(mac, sites) <- opt; site <- sites) { | |
println("%s %s".format(mac,site)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment