Mac Address Regex Java Validator

Search...

⌘K

Mac Address Regex Java Validator

Search...

⌘K


Mac Address Regex Java Validator

Mac Address Regex Java Validator

The MAC Address Regex Java Validator helps developers, testers, and network engineers validate MAC addresses using Java-compatible regex patterns. Whether you’re processing device logs, configuring routers, or cleaning up database entries, this tool ensures that each address follows standard MAC address formats.


You can also try these related Java validators:


bb:aa:dd:aa:55:55
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "bb:aa:dd:aa:55:55" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is a MAC Address?


A MAC (Media Access Control) address is a unique hardware identifier assigned to network interfaces. A typical MAC address contains six groups of two hexadecimal digits, separated by either colons (:) or hyphens (-). Validating MAC addresses is essential in applications dealing with network configurations, IoT devices, and security monitoring.


Java Regex Pattern for MAC Address


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


Explanation:

  • ^ and $: Anchors to ensure the pattern matches the entire string

  • ([0-9A-Fa-f]{2}[:-]){5}: Matches five groups of two hex characters followed by either : or -

  • ([0-9A-Fa-f]{2}): Final group of two hex characters

  • Accepts both formats: 01:23:45:67:89:AB and 01-23-45-67-89-AB


Java Code Example:


import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class MacAddressValidator {
    public static void main(String[] args) {
        String mac = "00:1A:2B:3C:4D:5E";
        String regex = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(mac);

        if (matcher.matches()) {
            System.out.println("Valid MAC address");
        } else {
            System.out.println("Invalid MAC address format");
        }
    }
}


Valid & Invalid Examples

Valid:

  • 00:1A:2B:3C:4D:5E

  • 01-23-45-67-89-ab

  • FF:FF:FF:FF:FF:FF


Invalid:

  • 001A2B3C4D5E (No separators)

  • ZZ:23:45:67:89:AB (Invalid characters)

  • 01:23:45:67:89 (Too short)

  • 01:23:45:67:89:AB:CD (Too long)


Pro Tips

  • Always sanitize user input before validating to avoid injection risks.

  • Use consistent separators (either : or -) in your backend systems for clean database storage.

  • MACs are hexadecimal; restrict input to 0–9 and A–F/a–f only.

  • Test edge cases like broadcast MAC (FF:FF:FF:FF:FF:FF) or multicast addresses.

  • Don’t confuse MAC addresses with IPs—MACs are static hardware-level identifiers.

  • The regex only checks format, not whether the MAC exists or is active.


Where It’s Used

  • Networking Software: Validate MACs in routers, switches, or DHCP tools

  • Inventory Systems: Ensure consistent MAC formats in device databases

  • IoT Applications: Track devices across hubs and ensure secure registration

  • Security Monitoring: Detect spoofed or malformed MAC addresses in logs


Combine with These Tools

Frequently asked questions

Can this regex detect spoofed MAC addresses?×
No. It only validates format, not authenticity.
Do MAC addresses always use colons?+
Are MAC addresses case-sensitive?+
Can this be used in Android or Spring Boot apps?+
How do I ensure consistent separators?+