In this instructional exercise we will see concerning how to approve MAC Address utilizing Java Regular Expression. The norm (IEEE 802) design for printing MAC-48 addresses in human-accommodating structure is six gatherings of two hexadecimal digits, isolated by dashes – or colons:
Table of Contents
ValidateMacAddress.java
package com.ehowtonow.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
In this instructional exercise we will see regarding how to approve MAC Address utilizing Java Regular Expression. The norm (IEEE 802) design for printing MAC-48 addresses in human-accommodating structure is six gatherings of two hexadecimal digits, isolated by dashes – or colons : .
Given string str, the errand is to check whether or not the given string is a legitimate MAC address by utilizing Regular Expression.
A legitimate MAC address should fulfill the accompanying conditions:
Input: str = “01-23-45-67-89-AB”;
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is a valid MAC address.
Input: str = “01-23-45-67-89-AH”;
Output: false
Explanation:
The given string contains ‘H’, the valid hexadecimal digits should be followed by letter from a-f, A-F, and 0-9. Therefore, it is not a valid MAC address.
Input: str = “01-23-45-67-AH”;
Output: false
Explanation:
The given string has five groups of two hexadecimal digits. Therefore, it is not a valid MAC address.
Learn CS Theory ideas for SDE interviews with the CS Theory Course at an understudy amicable cost and become industry prepared.
- It should contain 12 hexadecimal digits.
- One method for addressing them is to shape six sets of the characters isolated with a dash (- ) or colon(:). For
- instance, 01-23-45-67-89-AB is a substantial MAC address.
- One more method for addressing them is to shape three gatherings of four hexadecimal digits isolated by dots(.). For instance, 0123.4567.89AB is a substantial MAC address.
Approach: The thought is to utilize Regular Expression to tackle this issue. The accompanying advances can be followed to process the appropriate response.
Get the String.
Make an ordinary articulation to check substantial MAC address as referenced beneath:
regex = “^([0-9A-Fa-f]{2}[:- ]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})$”;
- Where:
^ addresses the beginning of the string. - ([0-9A-Fa-f]{2}[:- ]){5} addresses the five gatherings of two hexadecimal digits isolated by dashes (- ) or colons (:)
- ([0-9A-Fa-f]{2}) addresses the one gatherings of two hexadecimal digits.
- | addresses the or.
- ( addresses the beginning of the gathering.
- [0-9a-fA-F]{4}\\. addresses the initial segment of four hexadecimal digits isolated by spots (.).
- [0-9a-fA-F]{4}\\. addresses the second piece of four hexadecimal digits isolated by spots (.).
- [0-9a-fA-F]{4} addresses the third piece of four hexadecimal digits.
- ) addresses the closure of the gathering.
- $ addresses the closure of the string.
- Coordinate the given string with the Regular Expression. In Java, this should be possible by utilizing Pattern.matcher().
- Return valid in the event that the string coordinates with the given customary articulation, else return bogus.
// Java program to validate // MAC address using // regular expression import java.util.regex.*; class GFG { // Function to validate // MAC address // using regular expression public static boolean isValidMACAddress(String str) { // Regex to check valid // MAC address String regex = "^([0-9A-Fa-f]{2}[:-])" + "{5}([0-9A-Fa-f]{2})|" + "([0-9a-fA-F]{4}\\." + "[0-9a-fA-F]{4}\\." + "[0-9a-fA-F]{4})$" ; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the string is empty // return false if (str == null ) { return false ; } // Find match between given string // and regular expression // uSing Pattern.matcher() Matcher m = p.matcher(str); // Return if the string // matched the ReGex return m.matches(); } // Driver code public static void main(String args[]) { // Test Case 1: String str1 = "01-23-45-67-89-AB" ; System.out.println(isValidMACAddress(str1)); // Test Case 2: String str2 = "01:23:45:67:89:AB" ; System.out.println(isValidMACAddress(str2)); // Test Case 3: String str3 = "0123.4567.89AB" ; System.out.println(isValidMACAddress(str3)); // Test Case 4: String str4 = "01-23-45-67-89-AH" ; System.out.println(isValidMACAddress(str4)); // Test Case 5: String str5 = "01-23-45-67-AH" ; System.out.println(isValidMACAddress(str5)); } } |
true true true false false
Also Read: How to pop an alert message box using PHP?