How to validate MAC address using Regular Expression
Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression.
A valid MAC address must satisfy the following conditions:
- 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(.). For example, 0123.4567.89AB is a valid MAC address.
Examples:
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.
Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer.
- Get the String.
- Create a regular expression to check valid MAC address as mentioned below:
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:
- ^ represents the starting of the string.
- ([0-9A-Fa-f]{2}[:-]){5} represents the five groups of two hexadecimal digits separated by hyphens (-) or colons (:)
- ([0-9A-Fa-f]{2}) represents the one groups of two hexadecimal digits.
- | represents the or.
- ( represents the starting of the group.
- [0-9a-fA-F]{4}\\. represents the first part of four hexadecimal digits separated by dots (.).
- [0-9a-fA-F]{4}\\. represents the second part of four hexadecimal digits separated by dots (.).
- [0-9a-fA-F]{4} represents the third part of four hexadecimal digits.
- ) represents the ending of the group.
- $ represents the ending of the string.
- Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
- Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValidMACAddress(string str)
{
const regex pattern(
"^([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})$" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "01-23-45-67-89-AB" ;
cout << isValidMACAddress(str1) << endl;
string str2 = "01:23:45:67:89:AB" ;
cout << isValidMACAddress(str2) << endl;
string str3 = "0123.4567.89AB" ;
cout << isValidMACAddress(str3) << endl;
string str4 = "01-23-45-67-89-AH" ;
cout << isValidMACAddress(str4) << endl;
string str5 = "01-23-45-67-AH" ;
cout << isValidMACAddress(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidMACAddress(String str)
{
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})$" ;
Pattern p = Pattern.compile(regex);
if (str == null )
{
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "01-23-45-67-89-AB" ;
System.out.println(isValidMACAddress(str1));
String str2 = "01:23:45:67:89:AB" ;
System.out.println(isValidMACAddress(str2));
String str3 = "0123.4567.89AB" ;
System.out.println(isValidMACAddress(str3));
String str4 = "01-23-45-67-89-AH" ;
System.out.println(isValidMACAddress(str4));
String str5 = "01-23-45-67-AH" ;
System.out.println(isValidMACAddress(str5));
}
}
|
Python3
import re
def isValidMACAddress( str ):
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})$" )
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "01-23-45-67-89-AB"
print (isValidMACAddress(str1))
str2 = "01:23:45:67:89:AB"
print (isValidMACAddress(str2))
str3 = "0123.4567.89AB"
print (isValidMACAddress(str3))
str4 = "01-23-45-67-89-AH"
print (isValidMACAddress(str4))
str5 = "01-23-45-67-AH"
print (isValidMACAddress(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
public static bool isValidMACAddress( string str)
{
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})$" ;
Regex p = new Regex(regex);
if (str == null )
{
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "01-23-45-67-89-AB" ;
Console.WriteLine(isValidMACAddress(str1));
string str2 = "01:23:45:67:89:AB" ;
Console.WriteLine(isValidMACAddress(str2));
string str3 = "0123.4567.89AB" ;
Console.WriteLine(isValidMACAddress(str3));
string str4 = "01-23-45-67-89-AH" ;
Console.WriteLine(isValidMACAddress(str4));
string str5 = "01-23-45-67-AH" ;
Console.WriteLine(isValidMACAddress(str5));
}
}
|
Javascript
function isValidMACAddress(str) {
let regex = new RegExp(/^([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})$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "01-23-45-67-89-AB" ;
console.log(isValidMACAddress(str1));
let str2 = "01:23:45:67:89:AB" ;
console.log(isValidMACAddress(str2));
let str3 = "0123.4567.89AB" ;
console.log(isValidMACAddress(str3));
let str4 = "01-23-45-67-89-AH" ;
console.log(isValidMACAddress(str4));
let str5 = "01-23-45-67-AH" ;
console.log(isValidMACAddress(str5));
|
Output
true
true
true
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Last Updated :
22 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...