[英]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.