Programing

MAC 주소의 정규식은 무엇입니까?

crosscheck 2020. 7. 21. 07:53
반응형

MAC 주소의 정규식은 무엇입니까?


이 형식으로 :

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

또는:

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

사람에게 친숙한 형식으로 MAC-48 주소를 인쇄하기위한 표준 (IEEE 802) 형식은 하이픈 -또는 콜론으로 구분 된 두 개의 16 진수로 구성된 6 개의 그룹입니다 :.

그래서:

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

눈에 조금 힘들지만, 이것은 :

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

MAC 표기법에 대해 모든 콜론 또는 대시를 적용합니다.

( A1:B2-C3:D4-E5:F6예를 들어, 더 간단한 정규식 접근 방식은 위의 방법을 거부 할 수 있습니다.)


분리 문자 : " : ", " - ", " . "

이중 또는 단일 : 00 = 0, 0f = f

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

또는

/^([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 ...

이 정규식은 다음과 같은 Cisco 형식을 포함하여 거의 모든 Mac 형식과 일치합니다. 0102-0304-abcd

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

일치하는 문자열 예 :

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

혼합 형식도 일치합니다!


유니 코드 속성 \p{xdigit}에는 FULLWIDTH 버전이 포함되어 있습니다. \p{ASCII_Hex_Digit}대신 선호 할 수 있습니다 .

다음과 같이 입력하여 질문에 대한 답변이 가장 적합한 답변이 될 수 있습니다 (특정 CPAN 모듈이 설치되어있는 경우).

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

여기서 운이 좋은 패턴 번호 13으로 출력되는 특정 패턴을 보여줍니다 . 다른 많은 것들이 있습니다.

이 프로그램:

#!/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

이 출력을 생성합니다.

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

당신이 찾고있는 일종의 것 같습니다.


링크 가 도움 될 수 있습니다. 이것을 사용할 수 있습니다 :(([0-9A-Fa-f]{2}[-:]){5}[0-9A-Fa-f]{2})|(([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})


이 질문참조하십시오 .

다음과 같은 정규식 :

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

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

/^(([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})?$/

위의 정규식은 아래의 모든 mac 주소 유형을 확인합니다.

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

파이썬 버전은 다음과 같습니다.

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

PHP 개발자

filter_var($value, FILTER_VALIDATE_MAC)

유효성 검사를 위해 mac 주소를 전달하여 다음 절차를 사용할 수 있습니다.

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

PHP 사람들 :

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


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

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


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

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


As a MAC address can be 6 or 20 bytes (infiniband, ...) the correct answer is:

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

you can replace : with [:-.]? if you want different separators or none.


I don't think that the main RegEx is correct as it also classifies

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

as a valid MAC Address, even though it is not correct. The correct one would be:

((([a-zA-z0-9]{2}[-:]){5}([a-zA-z0-9]{2}))|(([a-zA-z0-9]{2}:){5}([a-zA-z0-9]{2})))

So that every time you can choose ':' or '-' for the whole MAC address.


This one is a small one:

(([0-9A-F]{2}[:-]?){6})

Have in mind, that weird mix of several chars or separators could pass.


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

참고URL : https://stackoverflow.com/questions/4260467/what-is-a-regular-expression-for-a-mac-address

반응형