Java RegEx – Validate MAC Address example shows how to validate a MAC address using a regex pattern in Java. There is a couple of regex patterns using which we can validate it.
How to validate a MAC address using a regex pattern in Java?
A Media Access Control address, commonly known as a MAC address, is a unique id assigned to a network device to use for communication within the network. The most common format of MAC address has 6 groups of 2 hexadecimal digits either separated by a hyphen or a colon as given below. Valid hexadecimal digits are 0 to 9 and a to f in upper or lower case.
1 2 |
AB:AC:12:3F:BD:C5 AB-AC-12-3F-BD-C5 |
Here is the regex pattern to validate that.
1 |
^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$ |
Where,
1 2 3 4 5 6 7 |
^ - Start of the string ( - Start of the group [0-9a-fA-F]{2} - Any character from 0 to 9, a to f, A to F 2 times [:-] - followed by either : or - ){5} - whole group 5 times [0-9a-fA-F]{2} - Any character from 0 to 9, a to f, A to F 2 times $ - End of the string |
Let’s try this regex with some sample MAC addresses.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.javacodeexamples.regex; public class RegExValidateMACAddress { public static void main(String[] args) { String[] strContent = { //only 10 digits "AB:AC:12:3F:BD", //14 digits "AB:AC:12:3F:BD:C5:EF", //X & Z are not valid hexadecimal numbers "AB:AC:12:3F:BD:XZ", //valid "AB:AC:12:3F:BD:C5", //valid "AB-AC-12-3F-BD-C5" }; String pattern = "^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$"; for(String str : strContent) { System.out.println( str + " => " + str.matches(pattern) ); } } } |
Output
1 2 3 4 5 |
AB:AC:12:3F:BD => false AB:AC:12:3F:BD:C5:EF => false AB:AC:12:3F:BD-XZ => false AB:AC:12:3F:BD:C5 => true AB-AC-12-3F-BD-C5 => true |
If the address has both “:” and “-” (e.g. “AB-AC-12:3F:BD:C5”), the above-given pattern will say it is valid, while it is not. In order to fix that, we need to change our pattern as given below.
1 |
^([0-9a-fA-F]{2}[:]){5}[0-9a-fA-F]{2}|([0-9a-fA-F]{2}[-]){5}[0-9a-fA-F]{2}$ |
I have basically joined two kinds of separator patterns using a regex OR condition (“|”).
Now, many manufactures do not follow these formats, for example, some displays MAC addresses without any separators. So if you want to cover that as well, here is the pattern that will validate the address in any format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
String[] strContent = { "AB:AC:12:3F:BD:C5", "AB-AC-12-3F-BD-C5", "ABAC123FBDC5", "ABA C12 3FB DC5", "AB AC 12 3F BD C5" }; String pattern = "^[0-9a-fA-F]{12}$"; for(String str : strContent) { System.out.println( str + " => " + str.replaceAll("[^0-9a-fA-F]", "").matches(pattern) ); } |
Output
1 2 3 4 5 |
AB:AC:12:3F:BD:C5 => true AB-AC-12-3F-BD-C5 => true ABAC123FBDC5 => true ABA C12 3FB DC5 => true AB AC 12 3F BD C5 => true |
Before matching the address with the pattern, I have replaced all non-hexadecimal characters with empty strings, thus removing them. The pattern is also simplified and just checks whether the address has 12 hexadecimal digits or not. As you can see from the output, regardless of the format, as long as the address has 12 valid hexadecimal digits the pattern will validate it as a valid address.
If you want to learn more about regex, please visit the Java regex tutorial.
Please let me know your views in the comments section below.