Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for validating 64 bit (8 byte) MAC addresses?

Tags:

java

regex

I have already gone through questions that have answered validating mac addresses but they are exclusive to 48-bit mac addresses. I am looking for a regex that can validate 8 bytes or 64-bit mac addresses. A 64-bit mac address looks something like this:(basically has 4 more hexadecimal digits than 48 bit)

00:13:a2:00:41:8b:93:7a
0013a200418b937a
AD:12:13:FC:14:EE:FF:FF
ad-12-13-fc-14-ee-ff-ad 

Based on answers for validating 48-bit mac addresses I have come up with this but am looking for something simpler.

^((([0-9A-Fa-f]{2}:){7})|(([0-9A-Fa-f]{2}-){7})|([0-9A-Fa-f]{14}))([0-9A-Fa-f]{2})$
like image 638
Shwetabh Shekhar Avatar asked May 08 '26 15:05

Shwetabh Shekhar


1 Answers

You could capture the separator with ([-:]?) which allows digits to be separated by a colon, dash, or nothing. Then for successive matches use a \1 backreference to ensure the separators are consistent. This will cut down on the repetitiveness.

^[0-9A-Fa-f]{2}([:-]?)(?:[0-9A-Fa-f]{2}\1){6}[0-9A-Fa-f]{2}$
like image 151
John Kugelman Avatar answered May 10 '26 09:05

John Kugelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!