How to validate a domain name using Regular Expression | C ++ program to validate the domain name using Regular Expression ; Function to validate the domain name . ; Regex to check valid domain name . ; If the domain name is empty return false ; Return true if the domain name 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 isValidDomain ( string str ) { const regex pattern ( " ^ ( ? ! - ) [ A - Za - z0-9 - ] + ( [ \\ - \\ . ] { 1 } [ a - z0-9 ] + ) * \\ . [ A - Za - z ] { 2,6 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " geeksforgeeks . org " ; cout << isValidDomain ( str1 ) << endl ; string str2 = " contribute . geeksforgeeks . org " ; cout << isValidDomain ( str2 ) << endl ; string str3 = " - geeksforgeeks . org " ; cout << isValidDomain ( str3 ) << endl ; string str4 = " geeksforgeeks . o " ; cout << isValidDomain ( str4 ) << endl ; string str5 = " . org " ; cout << isValidDomain ( str5 ) << endl ; return 0 ; }