Learn Beginner

HOW TO VALIDATE A DOMAIN NAME USING REGULAR EXPRESSION

How to validate a domain name using Regular Expression

In this technical and hacking era each and every developer used the validation 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:

  1. The domain name should be a-z or A-Z or 0-9 and hyphen (-).
  2. The domain name should be between 1 and 63 characters long.
  3. The domain name should not start or end with a hyphen(-) (e.g. -learnbeginner.com or learnbeginner.com-).
  4. The last TLD (Top level domain) must be at least two characters and a maximum of 6 characters.
  5. The domain name can be a subdomain (e.g. hindi.learnbeginner.com).

Examples:

Input: str = “hindi.learnbeginner.com”
Output: true
Explanation:
The given string satisfies all the above-mentioned conditions. Therefore it is a valid domain name.

Input: str = “-learnbeginner.com”
Output: false
Explanation:
The given string starts with a hyphen (-). Therefore it is not a valid domain name.

Input: str = “learnbeginner.o”
Output: false
Explanation:
The given string has the 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 = “.com”
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:

  1. Get the String.
  2. Create a regular expression to check 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.
  3. Match the given string with the regular expression. In Java, this can be done by using Pattern.matcher().
  4. Return true if the string matches with the given regular expression, else return false.
// 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 = "learnbeginner.com"; 
        System.out.println(isValidDomain(str1)); 
  
        // Test Case 2: 
        String str2 = "hindi.learnbeginner.com"; 
        System.out.println(isValidDomain(str2)); 
  
        // Test Case 3: 
        String str3 = "-learnbeginner.com"; 
        System.out.println(isValidDomain(str3)); 
  
        // Test Case 4: 
        String str4 = "learnbeginner.o"; 
        System.out.println(isValidDomain(str4)); 
  
        // Test Case 5: 
        String str5 = ".com"; 
        System.out.println(isValidDomain(str5)); 
    } 
}


Output:

true
true
false
false
false

Hiren Vaghasiya

Hiren Vaghasiya

Hiren is an avid technical geek and blog lover. Now a high time tech blogger here, along with Freelancer, Web Designer, Digital Marketer & Sales Funnel Specialist.

Add comment

Leave a Reply

Learn Beginner

Don't be shy, get in touch. We love meeting interesting learners and making the world's best developers.