What is a regular expression for a MAC Address?

2019-01-01 15:51发布

问题:

In this format:

3D:F2:C9:A6:B3:4F

or:

3D-F2-C9-A6-B3-4F

回答1:

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})$


回答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.)



回答3:

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


回答4:

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!



回答5:

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.



回答6:

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})



回答7:

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}$


回答8:

/(?:[A-Fa-f0-9]{2}[:-]){5}(?:[A-Fa-f0-9]{2})/


回答9:

The python version could be:

re.compile(r\'\\A(?:[\\da-f]{2}[:-]){5}[\\da-f]{2}\\Z\',re.I)


回答10:

/^(([a-fA-F0-9]{2}-){5}[a-fA-F0-9]{2}|([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}|([0-9A-Fa-f]{4}\\.){2}[0-9A-Fa-f]{4})?$/

The regex above validate all the mac addresses types below :

01-23-45-67-89-ab
01:23:45:67:89:ab
0123.4567.89ab


回答11:

for PHP developer

filter_var($value, FILTER_VALIDATE_MAC)


回答12:

You can use following procedure by passing mac address for validation,

private static final String MAC_PATTERN = \"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$\";

private boolean validateMAC(final String mac){          
    Pattern pattern = Pattern.compile(MAC_PATTERN);
    Matcher matcher = pattern.matcher(mac);
    return matcher.matches();             
}


回答13:

PHP Folks:

print_r(preg_match(\'/^(?:[0-9A-F]{2}[:]?){5}(?:[0-9A-F]{2}?)$/i\', \'00:25:90:8C:B8:59\'));

Need Explanation: http://regex101.com/r/wB0eT7



回答14:

If you need spaces between numbers, like this variant

3D : F2 : C9 : A6 : B3 : 4F

The regex changes to

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


回答15:

to match both 48-bit EUI-48 and 64-bit EUI-64 MAC addresses:

/\\A\\h{2}([:\\-]?\\h{2}){5}\\z|\\A\\h{2}([:\\-]?\\h{2}){7}\\z/

where \\h is a character in [0-9a-fA-F]

or:

/\\A[0-9a-fA-F]{2}([:\\-]?[0-9a-fA-F]{2}){5}\\z|\\A[0-9a-fA-F]{2}([:\\-]?[0-9a-fA-F]{2}){7}\\z/

this allows \'-\' or \':\' or no separator to be used



回答16:

Maybe the shortest possible:

/([\\da-f]{2}[:-]){5}[\\da-f]{2}/i

Update: A better way exists to validate MAC addresses in PHP which supports for both hyphen-styled and colon-styled MAC addresses. Use filter_var():

// Returns $macAddress, if it\'s a valid MAC address
filter_var($macAddress, FILTER_VALIDATE_MAC);

As I know, it supports MAC addresses in these forms (x: a hexadecimal number):

xx:xx:xx:xx:xx:xx
xx-xx-xx-xx-xx-xx
xxxx.xxxx.xxxx


回答17:

Thanks a lot to @Moshe for the great answer above. After doing some more research I would like to add my extra findings, both in regards to IEEE 802 and enforcing consistent separator usage in regex.

The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits, separated by hyphens -. It is however, widely adopted convention to also allow colon :, and three groups of four hexadecimal digits separated by periods ..

Full credit to @Moshe here for his initial statement, and to @pilcrow for pointing out that IEEE 802 only covers hypens.

Here is a regex that enforces that same separator is used throughout the mac address:

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

Regex101 demo

And here is an additional one that allows for use of no separator at all:

^(?:(?:[0-9A-Fa-f]{2}(?=([-:]|))(?:\\1[0-9A-Fa-f]{2}){5}))$

Regex101 demo



回答18:

the best answer is for mac address validation regex

^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$


标签: regex