728x90 AdSpace

Friday, 7 July 2017

Validate MAC Address using Java Regular Expression

MAC Address Validator Regular Expression Pattern
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 :.
Regular ExpressionDescription

for more regular expression syntax refer Java Regular Expression Syntax Descriptions 

package com.javatutorialcorner.regex;

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

public class ValidateMacAddress {

 private static final String PATTERN = "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$";

  public static void main(String[] args) {

   System.out.println("Validate MAC ADDRESS 00:00:00:00:AB:BC "+validate("00:00:00:00:AB:BC"));
   System.out.println("Validate MAC ADDRESS 00:00:00:EF:AB:BC "+validate("00:00:00:EF:AB:BC"));
   System.out.println("Validate MAC ADDRESS 00:00:DE:EF:AB:BC "+validate("00:00:DE:EF:AB:BC"));
   System.out.println("Validate MAC ADDRESS 00:GH:DE:EF:AB:BC "+validate("00:GH:DE:EF:AB:BC"));
   System.out.println("Validate MAC ADDRESS 0a:02:00:00:AB:BC "+validate("0a:02:00:00:AB:BC"));
   System.out.println("Validate MAC ADDRESS 00:00:0g:00:AB:BC "+validate("00:00:0g:00:AB:BC"));


  private static boolean validate(String password) {
   Pattern pattern = Pattern.compile(PATTERN);
   Matcher matcher = pattern.matcher(password);
   return matcher.matches();


Output : 
Validate MAC ADDRESS 00:00:00:00:AB:BC true
Validate MAC ADDRESS 00:00:00:EF:AB:BC true
Validate MAC ADDRESS 00:00:DE:EF:AB:BC true
Validate MAC ADDRESS 00:GH:DE:EF:AB:BC false
Validate MAC ADDRESS 0a:02:00:00:AB:BC true
Validate MAC ADDRESS 00:00:0g:00:AB:BC false
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Validate MAC Address using Java Regular Expression Rating: 5 Reviewed By: Annamalai Thangaraj