“updateDet”不应该识别为关键字“更新”

时间:2014-12-11 08:01:20

标签: regex scala parsing parser-combinators combinators

使用此代码

import scala.util.parsing.combinator.JavaTokenParsers

class TestKeywords  extends JavaTokenParsers {

  def keywords: Parser[String] = "update"

  def identifier: Parser[String] = not(keywords) ~> """[a-zA-Z0-9_$#]+""".r

  def script: Parser[Any] = repsep(identifier,",")
}

object TestKeywordsApp extends TestKeywords with App {
  val cmd = """updateDet,update"""
  parseAll(script,
    cmd.stripMargin) match {
    case Success(lup, _) => println(lup)
    case x => println(x)
  }
}

我收到错误

  

[1.1]失败:字符串匹配正则表达式\z' expected but u'结果

     

updateDet,更新

如何解决? updateDet不应该识别为关键字

scala 2.10.2

1 个答案:

答案 0 :(得分:0)

也许是单词边界                      - Amit Joki

要展开,您说identifiernot(keywords)后跟一些字符。但updateDet并非如此 - 它确实以关键字开头。也许您应该声明关键字以单词边界结尾(正则表达式\b)?                      - LMM