regex - scala parsing next line -
i've got text
--make_123 say:hello say:bye --make_123--
i need parse map:
{ makeid : 123 firstnode : hello secondnode : bye }
i've got following scala code:
class myparser extends regexparsers { def parsetext(input: string): mapcontent = parseall(parserequest, input) match { case success(result, _) => result case nosuccess(msg, _) => throw new someexception(msg) } def parserequest: parser[mapcontent] = parsemakeid ~ parsetext ^^ { case makeid ~ firstnode => { mapcontent( map("makeid" -> makeid) ++ map("firstnode" -> firstnode)) } } def parsemakeid: parser[string] = "--make_" ~> ".*".r def parsetext: parser[string] = "say:" ~> ".*".r } case class mapcontent(map: map[string, string])
well, here receive
string matching regex `.*\z$' expected `s' found
which second line , first literal of "say:"
how parse text? how omit empty line on 3rd line? thx
here assume, text parse contains 2 nodes , don't want check line --make_123--
valid (i.e., has same id first line).
you don't have omit empty line manually. gets skipped automatically, because it's whitespace.
you using parseall
, means parser should match whole text sent it. have add grammar code parse second line , closing line --make_123--
.
that's simple parser functions have defined, , slight modification function producing mapcontent
parsed result.
the parserequest
function should changed have following definition:
def parserequest: parser[mapcontent] = parsemakeid ~ parsetext ~ parsetext <~ parsemakeid ^^ { case makeid ~ firstnode ~ secondnode => mapcontent( map("makeid" -> makeid, "firstnode" -> firstnode, "secondnode" -> secondnode)) }
Comments
Post a Comment