我正在运行以下测试来验证汽车的车牌。我做错了什么?
testInvalid返回错误
testValid返回成功
@Test
fun testInvalid() {
val value = "ABC12345"
val pattern = Pattern.compile("[A-Z]{3}[0-9]{4}")
val matcher = pattern.matcher(value)
val found = matcher.find()
System.out.print("$value is valid: $found")
Assert.assertFalse(found)
}
@Test
fun testValid() {
val value = "ABC1234"
val pattern = Pattern.compile("[A-Z]{3}[0-9]{4}")
val matcher = pattern.matcher(value)
val found = matcher.find()
System.out.print("$value is valid: $found")
Assert.assertTrue(found)
}
答案 0 :(得分:0)
您需要在regEx末尾使用$字符来表示结束:
@Test
fun testInvalid() {
val value = "ABC12345"
val pattern = Pattern.compile("[A-Z]{3}[0-9]{4}$")
val matcher = pattern.matcher(value)
val found = matcher.find()
System.out.print("$value is valid: $found")
Assert.assertFalse(found)
}
@Test
fun testValid() {
val value = "ABC1234"
val pattern = Pattern.compile("[A-Z]{3}[0-9]{4}$")
val matcher = pattern.matcher(value)
val found = matcher.find()
System.out.print("$value is valid: $found")
Assert.assertTrue(found)
}