How to validate a domain name using Regular Expression

Given string str, the task is to check whether the given string is a valid domain name or not by using Regular Expression.
The valid domain name must satisfy the following conditions:
- The domain name should be a-z or A-Z or 0-9 and hyphen (-).
- The domain name should be between 1 and 63 characters long.
- The domain name should not start or end with a hyphen(-) (e.g. -zambiatek.com or zambiatek.com-).
- The last TLD (Top level domain) must be at least two characters and a maximum of 6 characters.
- The domain name can be a subdomain (e.g. write.zambiatek.com).
Examples:
Input: str = “write.zambiatek.com”
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is a valid domain name.
Input: str = “-zambiatek.com”
Output: false
Explanation:
The given string starts with a hyphen (-). Therefore, it is not a valid domain name.
Input: str = “zambiatek.o”
Output: false
Explanation:
The given string have last TLD of 1 character, the last TLD must be between 2 and 6 characters long. Therefore, it is not a valid domain name.
Input: str = “.org”
Output: false
Explanation:
The given string doesn’t start with a-z or A-Z or 0-9. Therefore, it is not a valid domain name.
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 the valid domain name as mentioned below:
regex = “^((?!-)[A-Za-z0-9-]{1, 63}(?<!-)\\.)+[A-Za-z]{2, 6}$”
- Where:
- ^ represents the starting of the string.
- ( represents the starting of the group.
- (?!-) represents the string should not start with a hyphen (-).
- [A-Za-z0-9-]{1, 63} represents the domain name should be a-z or A-Z or 0-9 and hyphen (-) between 1 and 63 characters long.
- (?<!-) represents the string should not end with a hyphen (-).
- \\. represents the string followed by a dot.
- )+ represents the ending of the group, this group must appear at least 1 time, but allowed multiple times for subdomain.
- [A-Za-z]{2, 6} represents the TLD must be A-Z or a-z between 2 and 6 characters long.
- $ 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++
// C++ program to validate the// domain name using Regular Expression#include <iostream>#include <regex>using namespace std;// Function to validate the domain name.bool isValidDomain(string str){ // Regex to check valid domain name. const regex pattern("^(?!-)[A-Za-z0-9-]+([\\-\\.]{1}[a-z0-9]+)*\\.[A-Za-z]{2,6}$"); // If the domain name // is empty return false if (str.empty()) { return false; } // Return true if the domain name // matched the ReGex if(regex_match(str, pattern)) { return true; } else { return false; }}// Driver Codeint main(){ // Test Case 1: string str1 = "zambiatek.com"; cout << isValidDomain(str1) << endl; // Test Case 2: string str2 = "write.zambiatek.com"; cout << isValidDomain(str2) << endl; // Test Case 3: string str3 = "-zambiatek.com"; cout << isValidDomain(str3) << endl; // Test Case 4: string str4 = "zambiatek.o"; cout << isValidDomain(str4) << endl; // Test Case 5: string str5 = ".org"; cout << isValidDomain(str5) << endl; return 0;}// This code is contributed by yuvraj_chandra |
Java
// Java program to validate domain name.// using regular expression.import java.util.regex.*;class GFG { // Function to validate domain name. public static boolean isValidDomain(String str) { // Regex to check valid domain name. String regex = "^((?!-)[A-Za-z0-9-]" + "{1,63}(?<!-)\\.)" + "+[A-Za-z]{2,6}"; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the string is empty // return false if (str == null) { return false; } // Pattern class contains matcher() // method to find the matching // between the given string and // regular expression. 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 = "zambiatek.com"; System.out.println(isValidDomain(str1)); // Test Case 2: String str2 = "write.zambiatek.com"; System.out.println(isValidDomain(str2)); // Test Case 3: String str3 = "-zambiatek.com"; System.out.println(isValidDomain(str3)); // Test Case 4: String str4 = "zambiatek.o"; System.out.println(isValidDomain(str4)); // Test Case 5: String str5 = ".org"; System.out.println(isValidDomain(str5)); }} |
Python3
# Python3 program to validate# domain name# using regular expressionimport re# Function to validate# domain name.def isValidDomain(str): # Regex to check valid # domain name. regex = "^((?!-)[A-Za-z0-9-]" + "{1,63}(?<!-)\\.)" +"+[A-Za-z]{2,6}" # Compile the ReGex p = re.compile(regex) # If the string is empty # return false if (str == None): return False # Return if the string # matched the ReGex if(re.search(p, str)): return True else: return False# Driver code# Test Case 1:str1 = "zambiatek.com"print(isValidDomain(str1))# Test Case 2:str2 = "write.zambiatek.com"print(isValidDomain(str2))# Test Case 3:str3 = "-zambiatek.com"print(isValidDomain(str3))# Test Case 4:str4 = "zambiatek.o"print(isValidDomain(str4))# Test Case 5:str5 = ".org"print(isValidDomain(str5)) |
C#
// C# program to validate domain name.// using Regular Expressionsusing System;using System.Text.RegularExpressions;class GFG{ // Main Method static void Main(string[] args) { // Input strings to Match // Valid Domain Name string[] str={"zambiatek.com","write.zambiatek.com","-zambiatek.com","zambiatek.o", ".org"}; foreach(string s in str) { Console.WriteLine( isValidDomain(s) ? "true" : "false"); } Console.ReadKey(); } // method containing the regex public static bool isValidDomain(string str) { string strRegex = @"^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,6}$"; Regex re = new Regex(strRegex); if (re.IsMatch(str)) return (true); else return (false); }}// This code is contributed by Rahul Chauhan |
Javascript
// Javascript program to validate// domain name using Regular Expression// Function to validate the// domain namefunction isValidDomain(str) { // Regex to check valid // Domain Name let regex = new RegExp(/^(?!-)[A-Za-z0-9-]+([\-\.]{1}[a-z0-9]+)*\.[A-Za-z]{2,6}$/); // if str // is empty return false if (str == null) { return "false"; } // Return true if the str // matched the ReGex if (regex.test(str) == true) { return "true"; } else { return "false"; }}// Driver Code// Test Case 1:let str1 = "zambiatek.com";console.log(isValidDomain(str1));// Test Case 2:let str2 = "contribute.zambiatek.com";console.log(isValidDomain(str2));// Test Case 3:let str3 = "-zambiatek.com";console.log(isValidDomain(str3));// Test Case 4:let str4 = "zambiatek.o";console.log(isValidDomain(str4));// Test Case 5:let str5 = ".org";console.log(isValidDomain(str5));// This code is contributed by Rahul Chauhan |
true true false false false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



