1

I'm trying to split input by some keywords without delimiter like white-space.

object MyParser extends JavaTokenParsers {
  def expr = (text | keyword).+
  def text = ".+".r ^^ ("'"+_+"'")
  def keyword = "ID".r ^^ ("["+_+"]")
}

val p = MyParser
p.parse(p.expr, "fooIDbar") match {
  case p.Success(r, _) => r foreach print
  case x => println(x.toString)
}

This outputs as below.

>> 'hogeIDfuga'

But I really want to do is like this.

>> 'hoge'[ID]'fuga'


It seems text engulfs all the characters. I tried to express [text does't contain keyword], but I could't. How to express it in regex or scala parser? or any other solutions?

I have seen some posts 1 2, but they don't work in my case.

Community
  • 1
  • 1
yakamoto
  • 50
  • 10

1 Answers1

3

First, keyword is a constant word so you don't need a regex, a plain string is enough. Second, a text is some string that doesn't contain a keyword, not any string. Try this:

import util.parsing.combinator._

object MyParser extends JavaTokenParsers {
  def expr = (text | keyword).+
  def text = """((?!ID).)+""".r ^^ ("'"+_+"'")
  def keyword = "ID" ^^ ("["+_+"]")
}

val p = MyParser
p.parse(p.expr, "fooIDbar") match {
  case p.Success(r, _) => r foreach print
  case x => println(x.toString)
}

As for the trick of writing a regex that not matching something, read this.

Community
  • 1
  • 1
xiefei
  • 6,563
  • 2
  • 26
  • 44