Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different MAC Addresses Regex

Tags:

java

regex

What is the correct regular expression for matching MAC addresses ? I googled about that but, most of questions and answers are incomplete. They only provide a regular expression for 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 :. However, this is not the real world case. Many routers, switches and other network devices vendors provide MAC addresses in formats like :

3D:F2:C9:A6:B3:4F //<-- standard 
3D-F2-C9-A6-B3:4F //<-- standard
3DF:2C9:A6B:34F   
3DF-2C9-A6B-34F
3D.F2.C9.A6.B3.4F
3df2c9a6b34f // <-- normalized

What I have until this moment is this:

public class MacAddressFormat implements StringFormat {
  @Override
  public String format(String mac) throws MacFormatException {
    validate(mac);
    return normalize(mac);
  }

  private String normalize(String mac) {

    return mac.replaceAll("(\\.|\\,|\\:|\\-)", "");
  }

  private void validate(String mac) {


    if (mac == null) {
      throw new MacFormatException("Empty MAC Address: !");
    }
    // How to combine these two regex together ? 
    //this one 
    Pattern pattern = Pattern.compile("^([0-9A-Fa-f]{2}[\\.:-]){5}([0-9A-Fa-f]{2})$");
    Matcher matcher = pattern.matcher(mac);
    // and this one ? 
    Pattern normalizedPattern = Pattern.compile("^[0-9a-fA-F]{12}$");
    Matcher normalizedMatcher = normalizedPattern.matcher(mac);

    if (!matcher.matches() && !normalizedMatcher.matches()) {
      throw new MacFormatException("Invalid MAC address format: " + mac);
    }

  }
}

How do yo combine the two regex in the code ?

like image 971
Adelin Avatar asked May 30 '13 06:05

Adelin


People also ask

How do you validate a MAC address?

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. Another way to represent them is to form three groups of four hexadecimal digits separated by dots(.).

What is multiline regex?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

Does regex include newline?

By default in most regex engines, . doesn't match newline characters, so the matching stops at the end of each logical line. If you want . to match really everything, including newlines, you need to enable “dot-matches-all” mode in your regex engine of choice (for example, add re. DOTALL flag in Python, or /s in PCRE.

Which one of the below is not a valid MAC address?

The correct answer is 00-11-20-M1-BC-B0, because the MAC address consists of 6 groups of Hexadecimal characters and the letter M in the 3rd group is the only character that is not a Hexadecimal character.


2 Answers

Why so much code? Here is how you can "normalize" your mac address:

mac.replaceAll("[^a-fA-F0-9]", "");

And here is a way to validate it:

public boolean validate(String mac) {
   Pattern p = Pattern.compile("^([a-fA-F0-9][:-]){5}[a-fA-F0-9][:-]$");
   Matcher m = p.matcher(mac);
   return m.find();
}
like image 122
AlexR Avatar answered Sep 22 '22 14:09

AlexR


Try this, working perfect..

A MAC address is a unique identifier assigned to most network adapters or network interface cards (NICs) by the manufacturer for identification, IEEE 802 standards use 48 bites or 6 bytes to represent a MAC address. This format gives 281,474,976,710,656 possible unique MAC addresses.

IEEE 802 standards define 3 commonly used formats to print a MAC address in hexadecimal digits:

Six groups of two hexadecimal digits separated by hyphens (-), like 01-23-45-67-89-ab

Six groups of two hexadecimal digits separated by colons (:), like 01:23:45:67:89:ab

Three groups of four hexadecimal digits separated by dots (.), like 0123.4567.89ab

public boolean macValidate(String mac) {
        Pattern p = Pattern.compile("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$");
        Matcher m = p.matcher(mac);
        return m.find();
}
like image 34
Neeraj Singh Avatar answered Sep 23 '22 14:09

Neeraj Singh