How to validate MAC address using Regular Expression | C ++ program to validate the MAC address using Regular Expression ; Function to validate the MAC address ; Regex to check valid MAC address ; If the MAC address is empty return false ; Return true if the MAC address matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE 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 ; }