Skip to content

Is MAC48 Address?

A media access control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment.

The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits (0 to 9 or A to F), separated by hyphens (e.g. 01-23-45-67-89-AB).

Your task is to check by given string inputString whether it corresponds to MAC-48 address or not.

Example

  • For inputString = "00-1B-63-84-45-E6", the output should be

    is_mac48_address(inputString) = true
  • For inputString = "Z1-1B-63-84-45-E6", the output should be

    is_mac48_address(inputString) = false
  • For inputString = "not a MAC-48 address", the output should be

    is_mac48_address(inputString) = false

Solution

py
import string


def is_mac48_address(input_string):
    input_string = input_string.split('-')

    if len(input_string) != 6:
        return False

    for i in input_string:
        if len(i) != 2 or (not all(c in string.hexdigits for c in i)):
            return False

    return True


print(is_mac48_address('00-1B-63-84-45-E6'))
print(is_mac48_address('ZZ-1B-63-84-45-Z6'))
print(is_mac48_address('not a mac-address'))
js
function isMAC48Address(inputString) {
  const regex = /^[0-9A-F]{2}(-[0-9A-F]{2}){5}$/;
  return regex.test(inputString);
}

console.log(isMAC48Address('00-1B-63-84-45-E6'));

my thoughts are neither my employer's nor my wife's