簡體   English   中英

Scala Regex小於等於運算符(<=)

[英]Scala Regex for less than equal to operator (<=)

我試圖用(<,<=,> =,>)解析表達式。 除<=之外的所有工作都很好。 有人可以幫助解決問題。 碼:

object MyTestParser extends RegexParsers {
  override def skipWhitespace = true

  private val expression: Parser[String] = """[a-zA-Z0-9\.]+""".r

  val operation: Parser[Try[Boolean]] =
    expression ~ ("<" | "<=" | ">=" | ">") ~ expression ^^ {
      case v1 ~ op ~ v2 => for {
        a <- Try(v1.toDouble)
        b <- Try(v2.toDouble)
      } yield op match {
        case "<" => a < b
        case "<=" => a <= b
        case ">" => a > b
        case ">=" => a >= b
      }
  }
}

測試:

"MyTestParser" should {
    "successfully parse <= condition" in {
      val parser = MyTestParser.parseAll(MyTestParser.operation, "10 <= 20")
      val result = parser match {
        case MyTestParser.Success(s, _) => s.get
        case MyTestParser.Failure(e, _) =>
          println(s"Parsing failed with error: $e")
          false
        case MyTestParser.Error(e, _) =>
          println(s"Parsing error: $e")
          false
      }
      result === true
    }

    "successfully parse >= condition" in {
      val result = MyTestParser.parseAll(MyTestParser.operation, "50 >= 20").get
      result === scala.util.Success(true)
    }
  }

<=條件的錯誤:

Parsing failed with error: string matching regex `[a-zA-Z0-9\.]+' expected but `=' found

您需要更改備選項的順序,以便可以首先檢查最長的選項。

expression ~ ( "<=" | ">=" | ">" | "<") ~ expression ^^ {

如果最短的替代方案首先匹配,則根本不考慮其他方案。

另請注意,句點不必在字符類中進行轉義,這樣做:

"""[a-zA-Z0-9.]+""".r

您的問題是“<”與<=匹配,因此它繼續嘗試表達式。 如果您更改順序以便首先出現“<=”,那么將匹配,您將獲得所需的結果。

@Prateek:它不起作用,因為正則表達式引擎就像一個布爾OR。 如果在某個點滿足or鏈中的一個模式,則它不會進一步搜索。

所以,使用時| 在模式之間, 如果兩個或多個模式具有共同的子字符串 ,則必須先放置最長的模式

作為一般規則從最長到最短的順序排列模式

像這樣更改相關行使其工作:

 // It works as expected with '>= / >' also before for the same reason
 expression ~ ("<=" | "<" | ">=" | ">") ~ expression ^^ {

或者你想遵循一般規則

 expression ~ ("<=" | ">=" | "<" | ">") ~ expression ^^ {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM