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
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();
}
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
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.
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();
}
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();
}
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
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.