[英]Scala REGEX match for MAC address
Good evening Stackoverflow, 晚上好,Stackoverflow,
I am stuck in a spot where I can't get Scala regex matches to play nice, here is my code 我被困在无法让Scala regex比赛发挥出色的地方,这是我的代码
private def handle_read(packet: TFTPReadRequestPacket, tftp_io: TFTP): Unit = {
val MAC_REGEX = "([0-9A-F]{2}[:-]){5}([0-9A-F]{2})".r
packet.getFilename match {
case MAC_REGEX(a) => println(s"Client is coming from $a")
}
}
When the regex is ([0-9A-F]{2}[:-])
and I request for the file 70-
it is fine and spits out that the client is "coming from 70", but when it is the full regex and I request 70-CD-60-74-24-9C
it throws an exception like such 当正则表达式为([0-9A-F]{2}[:-])
,我请求文件70-
很好,并吐出客户端“来自70”,但是当它已满时正则表达式,我要求70-CD-60-74-24-9C
它会引发类似这样的异常
[ERROR] [04/28/2015 21:25:27.818] [polydeploy-baremetal-akka.actor.default-dispatcher-4] [akka://polydeploy-baremetal/user/TFTP_Queue] 70-CD-60-74-24-9C (of class java.lang.String)
scala.MatchError: 70-CD-60-74-24-9C (of class java.lang.String)
at com.polydeploy.baremetal.TFTPQueue$.handle_read(TFTPQueue.scala:40)
at com.polydeploy.baremetal.TFTPQueue$.com$polydeploy$baremetal$TFTPQueue$$handle_request(TFTPQueue.scala:33)
at com.polydeploy.baremetal.TFTPQueue$$anonfun$receive$1.applyOrElse(TFTPQueue.scala:14)
at akka.actor.Actor$class.aroundReceive(Actor.scala:467)
at com.polydeploy.baremetal.TFTPQueue$.aroundReceive(TFTPQueue.scala:10)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)
at akka.actor.ActorCell.invoke(ActorCell.scala:487)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:254)
at akka.dispatch.Mailbox.run(Mailbox.scala:221)
at akka.dispatch.Mailbox.exec(Mailbox.scala:231)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
What I am wanting to try and accumplish is to be able to have a TFTP request come in for pxelinux.cfg/01-70-CD-60-74-24-9C
and pull out the MAC address. 我想尝试并举例说明的是能够向pxelinux.cfg/01-70-CD-60-74-24-9C
发出TFTP请求,并取出MAC地址。
Any and all help is greatly appreciated! 任何帮助都将不胜感激!
Thanks, Liam. 谢谢,利亚姆。
When the regex is
([0-9A-F]{2}[:-])
and I request for the file70-
it is fine 当正则表达式为([0-9A-F]{2}[:-])
并且我请求文件70-
就可以了
This is because, in this case, your regex contains a single group. 这是因为在这种情况下,您的正则表达式包含一个组。
This worked for me: 这对我有用:
val MAC_REGEX = "(([0-9A-F]{2}[:-]){5}([0-9A-F]{2}))".r
"70-CD-60-74-24-9C" match {
case MAC_REGEX(a, _*) => println(s"Client is coming from $a")
}
// prints "Client is coming from 70-CD-60-74-24-9C"
It works because I wrapped the entire regex with a group. 之所以有效,是因为我将整个正则表达式包装在一起。 a
captures that outer group and _*
is a sequence of ignored matches for all the other groups. a
捕获外部组,而_*
是所有其他组的忽略匹配序列。 Apparently Regex
's extractor returns a list with an element for each capture group. 显然, Regex
的提取器返回一个列表,其中包含每个捕获组的元素。
I have a feeling there is a better way to do this though... 我觉得有更好的方法可以做到这一点...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.