Created
April 30, 2014 16:04
-
-
Save letusfly85/f936da8f986f8bc60351 to your computer and use it in GitHub Desktop.
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
/** | |
* http://stackoverflow.com/questions/19009299/scala-parser-combinators-consume-until-match | |
* | |
* | |
*/ | |
import scala.util.parsing.combinator.RegexParsers | |
object MyParser extends RegexParsers { | |
val beginToken: Parser[String] = "begin" | |
val dontCareToken: Parser[String] = "DONT CARE" | |
val text: Parser[String] = not(dontCareToken) ~> """([^\n]+)""".r | |
val dontCareText: Parser[String] = not(beginToken) ~> """([^\n]+)""".r | |
val skipDontCare = dontCareToken ~ dontCareText ^^ { case c => "" } | |
val document: Parser[String] = | |
beginToken ~> text.+ <~ opt(skipDontCare) ^^ { | |
_.mkString("\n") | |
} | |
val documents: Parser[Iterable[String]] = document.+ | |
} | |
val s = """begin | |
Text I care about | |
Text I care about | |
DONT CARE | |
Text I don't care about | |
begin | |
More text I care about | |
""" | |
MyParser.parseAll(MyParser.documents,s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment