Search code examples
regexscalamac-addresstftp

Scala REGEX match for MAC address


Good evening Stackoverflow,

I am stuck in a spot where I can't get Scala regex matches to play nice, here is my code

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

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

Any and all help is greatly appreciated!

Thanks, Liam.


Solution

  • When the regex is ([0-9A-F]{2}[:-]) and I request for the file 70- it is fine

    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. Apparently Regex's extractor returns a list with an element for each capture group.

    I have a feeling there is a better way to do this though...