0

A "valid" MAC address is 6 groups of hexadecimal character pairs, separated by a dash or colon.

Example:

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

However, "invalid" MAC addresses contain hex pairs that are 00, FF, 88 or 87.

Example:

00-00-00-00-00-00
FF-FF-FF-FF-FF-FF
88-88-88-88-87-88

This is what I got so far:

(?!8)([0-9A-F]{2})([:-][0-9A-F]{2}){5}

But for some reason I don't know why, I cannot use:

(?!88|87|FF|00)

For the negated portion of the regex.

NOTE: Not a duplicate SO entry because this looks for invalid hex pairs and excludes them.

Vippy
  • 1,122
  • 2
  • 17
  • 29
  • Do the rules specify that no single byte value can be `00`, `FF`, `88`, or `87` or that the entire string cannot be `87-87-87-87-87-87`? – Dai Aug 26 '15 at 23:32
  • 1
    No, this question has a specific additional rule for 4 byte patterns, and it's exactly that pattern which causes the problem here. – MSalters Aug 26 '15 at 23:35
  • Why do you have to do it in a single regex? Validate the MAC address, and then test for invalid combinations using capture groups from that first test. – Ken White Aug 26 '15 at 23:43
  • @Dai that would be the case for the zeros and F's, but not for 88 or 87, so it requires us to check each 2 chars in-between the colon or dash. hrbrmstr: that one is not a duplicate for this post, technically the invalid ones are "kosher" Mac address that aren't set correctly. KenWhite: I am trying to validate the MAC address, I don't care how the regex is written (groups or not). – Vippy Aug 27 '15 at 00:05

1 Answers1

2

Use a negative look ahead on the entire input:

^((?!00|FF|88|87)[0-9A-F]{2}([:-]|$)){6}$

See live demo.

Note how your regex can be simplified by using an alternation for a separator or end of input.

Vippy
  • 1,122
  • 2
  • 17
  • 29
Bohemian
  • 365,064
  • 84
  • 522
  • 658
  • Flipping awesome!!! Especially the separator or end of input that way you can repeat this group exactly the number of times that is required. You rock!!! Thank you. – Vippy Aug 27 '15 at 15:28
  • @Vippy your edit changes the match, so I just want to check: Is `FF-F2-C9-A6-88-4F` "valid" (some terms "good", some "bad")? – Bohemian Aug 27 '15 at 23:06