1

For my application, I require a RegEx which can find out if a string is at least partially a MAC address, but so far I have only found the following RegEx which only finds if something is a complete MAC address

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

What would be a RegEx (possibly derived from the one stated above) which returns a full match for these 3 strings:

  1. 93:31:13:E1:D3:1A
  2. 3E-EB-47-8
  3. 84:0C:

while not returning a match for these 2 strings:

  1. 6B-90-8Y-31-D1-7K
  2. 37-4H-31

Note: I would not say this is a "practical" duplicate of THIS question as

  1. That question was never answered successfully despite it being ~6 years old
  2. That question is directed towards a specific engine
  3. None of the answers provided to that question work in all/any of the cases above
Nope Nope
  • 75
  • 8
  • Try [`^[0-9A-Fa-f]{1,2}(?:[:-][0-9A-Fa-f]{1,2})*[:-]?$`](https://regex101.com/r/1MVDWG/1). Note it will also match `3E:EB-47-8`. – Wiktor Stribiżew Mar 06 '18 at 20:04
  • Ha, had almost the same: [`^[0-9A-Fa-f]{0,2}([:-][0-9A-Fa-f]{1,}){0,5}[:-]?$`](https://regex101.com/r/DAASxo/1) – wp78de Mar 06 '18 at 20:07
  • @WiktorStribiżew Almost perfect, however, this matches infinitely long MAC addresses – Nope Nope Mar 06 '18 at 20:08
  • So, what are other requirements you have not revealed yet? Looks like you need to study [limiting quantifiers](https://www.regular-expressions.info/repeat.html#limit). What length restrictions do you have in mind? BTW, here is a version with only 1 delimiter support and only up to 6 parts in the MAC address: [`^[0-9A-Fa-f]{1,2}(?:(?::[0-9A-Fa-f]{0,2}){0,5}:?|(?:-[0-9A-Fa-f]{0,2}){0,5}-?)$`](https://regex101.com/r/1MVDWG/3) – Wiktor Stribiżew Mar 06 '18 at 20:08
  • @WiktorStribiżew Well, It is meant to match both partial MAC addresses, and complete MAC addresses, which from what I can find are at most "XX:XX:XX:XX:XX:XX" or 2 characters*6 fields, the RegEx you included works perfectly except for the fact that it allows 1 too many ":" – Nope Nope Mar 06 '18 at 20:14
  • @NopeNope You may want to have a look at my final answer too. – wp78de Mar 06 '18 at 21:56

1 Answers1

3

If you want to match partial MAC addresses with just 1 type of delimiter, you may use

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

See the regex demo.

To match MAC addresses with both - and : mixed up in the string, you may use

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

See this regex demo.

Regex details

MAC address here can consist of 6 : or - separated 2-hex char parts, and matches it as one types.

  • ^ - start of the string
  • [0-9A-Fa-f]{1,2} - 1 or 2 hex chars
  • (?:
    • (?::[0-9A-Fa-f]{0,2}){0,5} - 0 to 5 repetitions of a : followed with 0 to 2 hex chars
    • | - or
    • (?:-[0-9A-Fa-f]{0,2}){0,5} - 0 to 5 repetitions of a - followed with 0 to 2 hex chars
  • ) - end of the non-capturing alternation group
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563