Different MAC Addresses Regex

后端 未结 7 788
旧时难觅i
旧时难觅i 2021-01-06 08:30

What is the correct regular expression for matching MAC addresses ? I googled about that but, most of questions and answers are incomplete. They only provide a regular expre

相关标签:
7条回答
  • 2021-01-06 09:01

    Why so much code? Here is how you can "normalize" your mac address:

    mac.replaceAll("[^a-fA-F0-9]", "");

    And here is a way to validate it:

    public boolean validate(String mac) {
       Pattern p = Pattern.compile("^([a-fA-F0-9][:-]){5}[a-fA-F0-9][:-]$");
       Matcher m = p.matcher(mac);
       return m.find();
    }
    
    0 讨论(0)
  • 2021-01-06 09:03

    I know this question is kind of old but i think there is not a really good solution for the problem:

    Two steps to do

    1. Change the Mac String to parse to the format of your choice (e.g. xx:xx:xx:xx:xx:xx)
    2. Write a method which validates a string with the format above

    Example:

    1)

    you can easily achieve this by calling

    public static String parseMac(String mac) {
        mac = mac.replaceAll("[:.-]", "");
    
        String res = "";
        for (int i = 0; i < 6; i++) {
            if (i != 5) {
                res += mac.substring(i * 2, (i + 1) * 2) + ":";
            } else {
                res += mac.substring(i * 2);
            }
        }
        return res;
    }
    

    :.- are the chars which will be removed from the string

    2)

    public static boolean isMacValid(String mac) {
        Pattern p = Pattern.compile("^([a-fA-F0-9]{2}[:-]){5}[a-fA-F0-9]{2}$");
        Matcher m = p.matcher(mac);
        return m.find();
    }
    

    Ofc you can make the Pattern a local class variable to improve the efficiency of your code.

    0 讨论(0)
  • 2021-01-06 09:08

    MAC Address can be validated with the below code:

    private static final String PATTERN = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";
        
    private static boolean validateMAC(String mac) {
            Pattern pattern = Pattern.compile(PATTERN);
            Matcher matcher = pattern.matcher(mac);
            return matcher.matches();
        }
    
    0 讨论(0)
  • 2021-01-06 09:15

    Try this, working perfect..

    A MAC address is a unique identifier assigned to most network adapters or network interface cards (NICs) by the manufacturer for identification, IEEE 802 standards use 48 bites or 6 bytes to represent a MAC address. This format gives 281,474,976,710,656 possible unique MAC addresses.

    IEEE 802 standards define 3 commonly used formats to print a MAC address in hexadecimal digits:

    Six groups of two hexadecimal digits separated by hyphens (-), like 01-23-45-67-89-ab

    Six groups of two hexadecimal digits separated by colons (:), like 01:23:45:67:89:ab

    Three groups of four hexadecimal digits separated by dots (.), like 0123.4567.89ab

    public boolean macValidate(String mac) {
            Pattern p = Pattern.compile("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$");
            Matcher m = p.matcher(mac);
            return m.find();
    }
    
    0 讨论(0)
  • 2021-01-06 09:20

    MAC addresses can be 6 or 20 bytes (infiniband)

    The correct regexp assuming the separator is : is

    ^([0-9A-Fa-f]{2}:){5}(([0-9A-Fa-f]{2}:){14})?([0-9A-Fa-f]{2})$
    

    We match 5 times XX: optionally we match another 14 times XX: and we match XX

    0 讨论(0)
  • 2021-01-06 09:21

    Below Regex Pattern will help to manage all kinds of the above listed patterns

    "([MACmac]?){1}([:.-]?){1}(([0-9A-Fa-f]{2,3}[:.-]?){4,6})"
    

    here at end i added {4,6} because some pattern as requested below is expecting the group of 3 for 4 time. else if you are looking ONLY for the default on which is usually group of 2 characters 6 times then you can use the below one.

    "([MACmac]?){1}([:.-]?){1}(([0-9A-Fa-f]{2}[:.-]?){6})"
    

    hope this helps all.

    0 讨论(0)
提交回复
热议问题