Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a regular expression for a MAC Address?

Tags:

regex

People also ask

What is a valid format for a MAC address?

A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits. One way to represent them is to form six pairs of the characters separated with a hyphen (-) or colon(:). For example, 01-23-45-67-89-AB is a valid MAC address.

What is this regular expression?

A Regular Expression (or Regex) is a pattern (or filter) that describes a set of strings that matches the pattern. In other words, a regex accepts a certain set of strings and rejects the rest.

What characters can be in a MAC address?

A MAC address consists of 48 bits, usually represented as a string of 12 hexadecimal digits (0 to 9, a to f, or A to F); these are often grouped into pairs separated by colons or dashes. For example, the MAC address 001B638445E6 may be given as 00:1b:63:84:45:e6 or as 00-1B-63-84-45-E6.


The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits, separated by hyphens - or colons :.

So:

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

A little hard on the eyes, but this:

/^(?:[[:xdigit:]]{2}([-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}$/

will enforce either all colons or all dashes for your MAC notation.

(A simpler regex approach might permit A1:B2-C3:D4-E5:F6, for example, which the above rejects.)


This regex matches pretty much every mac format including Cisco format such as 0102-0304-abcd

^([[:xdigit:]]{2}[:.-]?){5}[[:xdigit:]]{2}$

Example strings which it matches:

01:02:03:04:ab:cd
01-02-03-04-ab-cd
01.02.03.04.ab.cd
0102-0304-abcd
01020304abcd

Mixed format will be matched also!


delimiter: ":","-","."

double or single: 00 = 0, 0f = f

/^([0-9a-f]{1,2}[\.:-]){5}([0-9a-f]{1,2})$/i

or

/^([0-9a-F]{1,2}[\.:-]){5}([0-9a-F]{1,2})$/


exm: 00:27:0e:2a:b9:aa, 00-27-0E-2A-B9-AA, 0.27.e.2a.b9.aa ...

Be warned that the Unicode property \p{xdigit} includes the FULLWIDTH versions. You might prefer \p{ASCII_Hex_Digit} instead.

The answer to the question asked might be best answered — provided you have a certain venerable CPAN module installed — by typing:

% perl -MRegexp::Common -lE 'say $RE{net}{MAC}'

I show the particular pattern it outputs here as lucky pattern number 13; there are many others.

This program:

#!/usr/bin/env perl
use 5.010;
use strict;
use warnings qw<FATAL all>;

my $mac_rx = qr{
    ^ (?&MAC_addr) $
    (?(DEFINE)
        (?<MAC_addr>
                (?&pair) (?<it>  (?&either) )
            (?: (?&pair) \k<it> ) {4}
                (?&pair)
        )
        (?<pair>    [0-9a-f] {2} )
        (?<either>  [:\-]        )
    )
}xi;

while (<DATA>) {
    chomp;
    printf("%-25s %s\n", $_ => /$mac_rx/ ? "ok" : "not ok");
}

__END__
3D:F2:C9:A6:B3:4F
3D:F2:AC9:A6:B3:4F
3D:F2:C9:A6:B3:4F:00
:F2:C9:A6:B3:4F
F2:C9:A6:B3:4F
3d:f2:c9:a6:b3:4f
3D-F2-C9-A6-B3-4F
3D-F2:C9-A6:B3-4F

generates this output:

3D:F2:C9:A6:B3:4F         ok
3D:F2:AC9:A6:B3:4F        not ok
3D:F2:C9:A6:B3:4F:00      not ok
:F2:C9:A6:B3:4F           not ok
F2:C9:A6:B3:4F            not ok
3d:f2:c9:a6:b3:4f         ok
3D-F2-C9-A6-B3-4F         ok
3D-F2:C9-A6:B3-4F         not ok

Which seems the sort of thing you're looking for.


This link might help you. You can use this : (([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2})|(([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})


See this question also.

Regexes as follows:

^[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}$

^[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}$