Regex MAC address

Hey guys,

I want to use the Regexp library to check if a MAC address could be valid, is that possible and can I use the standard Regex formula for that?

void loop() {
  MatchState ms;
  int packetSize = UDP.parsePacket();
  if (packetSize)
  {
    // receive incoming UDP packets
    Serial.printf("Received %d bytes from %s, port %d\r\n", packetSize, UDP.remoteIP().toString().c_str(), UDP.remotePort());
    int len = UDP.read(incomingPacket, 255);
    if (len > 0)
    {
      incomingPacket[len] = 0;
    }
    ms.Target (incomingPacket);
    char result = ms.Match ("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"); 
    Serial.println(result);
    Serial.printf("UDP packet contents: %s\n", incomingPacket);

    // send back a reply, to the IP address and port we got the packet from
    UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
    UDP.write(replyPacket);
    UDP.endPacket();
  }
} 

result is null when being printed.

No need to go drastic regex on such rudimentary problem, sscanf should be sufficient Convert String to Mac Address

1 Like