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