-
-
Save clhodapp/2875508 to your computer and use it in GitHub Desktop.
Scala RegEx Parser Demo
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
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 | |
} | |
def 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