Skip to content

Instantly share code, notes, and snippets.

@chbaranowski
Forked from clhodapp/KeyValueParser.scala
Created June 23, 2012 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chbaranowski/2977170 to your computer and use it in GitHub Desktop.
Save chbaranowski/2977170 to your computer and use it in GitHub Desktop.
Scala RegEx Parser Demo
import scala.util.parsing.combinator.RegexParsers
/**
* Simple parser for a key value string like:
* key01 = Value01 key02=value02 key03 =value03 key04= value04
*
* The parser does not support whitespace in key or values.
*/
object KeyValueParser extends RegexParsers {
def map = rep(mapping) ^^ {_.toMap}
def mapping = word ~ "=" ~ word ^^ {
case key ~ _ ~ value => key -> value
}
val word = "\\w+".r
/**
* Parse a key value text into a Map[String, String].
*/
def parse(input: String) = parseAll(map, input) match {
case Success(result,_) => result
case failure : NoSuccess => throw new Exception(failure.msg)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment