[英]Scala regex pattern match of ip address
我不明白为什么这段代码返回false:
val reg = """.*(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}).*""".r
"ttt20.30.4.140ttt" match{
case reg(one, two, three, four) =>
if (host == one + "." + two + "." + three + "." + four) true else false
case _ => false
}
并且仅当我将其更改为:
val reg = """.*(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}).*""".r
"20.30.4.140" match{
case reg(one, two, three, four) =>
if (host == one + "." + two + "." + three + "." + four) true else false
case _ => false
}
它确实匹配
def main( args: Array[String] ) : Unit = {
val regex = """.*(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}).*""".r
val x = "ttt20.30.4.140ttt"
x match {
case regex(ip1,ip2,ip3,ip4) => println(ip1, ip2, ip3, ip4)
case _ => println("No match.")
}
}
匹配,但不符合您的预期。 结果将是(0,30,4,140)代替(20,30,4,140)。 如您所见.*
是贪婪的,因此会消耗尽可能多的输入。
例如ab12
可以通过.*(\\d{1,3})
分隔为
ab
和12
ab1
和2
....这是选择的变体,因为.*
会消耗尽可能多的输入 使.*
不愿意(而不是贪婪),也就是.*?
所以总共
""".*?(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3}).*""".r
精确定义第一个数字之前的模式,例如,如果这些仅是字符,请执行
"""[a-zA-Z]*(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3}).*""".r
您应该使用勉强的量词而不是贪婪的量词 :
val reg = """.*?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}).*""".r
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.