我需要一个Scala正则表达式才能匹配0.0或0.00或0.000。 我怎样才能做到这一点?
我已经尝试了下面的scala正则表达式,但它没有按预期工作
val reg = "0\\.0{1,}".r
对于所有测试用例,此正则表达式始终返回TRUE
Testcase-1: - 失败
val str = "0."
val matchFound = reg.findFirstMatchIn(str).isEmpty
matchFound: Boolean = true
Testcase-2: - 失败
val str = "0.89"
val matchFound = reg.findFirstMatchIn(str).isEmpty
matchFound: Boolean = true
Testcase-3: - 失败
val str = "0.abc"
val matchFound = reg.findFirstMatchIn(str).isEmpty
matchFound: Boolean = true
答案 0 :(得分:1)
我认为你错误解释了你的考试。您测试匹配是否为空,并且为true,这意味着它始终为空:
val reg = "0\\.0{1,}".r
val testcases = List ("0.0", "0.00", "0.000", "0.", "0.89", "0.abc")
testcases.foreach (t => println (t + ":\t" + reg.findFirstMatchIn(t).isEmpty))
0.0: false
0.00: false
0.000: false
0.: true
0.89: true
0.abc: true