Skip to content

Instantly share code, notes, and snippets.

@mikemckibben
Created January 11, 2013 16:46
Show Gist options
  • Save mikemckibben/4512173 to your computer and use it in GitHub Desktop.
Save mikemckibben/4512173 to your computer and use it in GitHub Desktop.
Example of regex extractor with pattern matching in scala.
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