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. -geeksforgeeks.org or geeksforgeeks.org-).
- 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.geeksforgeeks.org).
Examples:
Input: str = “write.geeksforgeeks.org”
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is a valid domain name.
Input: str = “-geeksforgeeks.org”
Output: false
Explanation:
The given string starts with a hyphen (-). Therefore, it is not a valid domain name.
Input: str = “geeksforgeeks.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++
#include <iostream>
#include <regex>
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 = "write.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;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidDomain(String str)
{
String regex = "^((?!-)[A-Za-z0-9-]"
+ "{1,63}(?<!-)\\.)"
+ "+[A-Za-z]{2,6}" ;
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 = "geeksforgeeks.org" ;
System.out.println(isValidDomain(str1));
String str2 = "write.geeksforgeeks.org" ;
System.out.println(isValidDomain(str2));
String str3 = "-geeksforgeeks.org" ;
System.out.println(isValidDomain(str3));
String str4 = "geeksforgeeks.o" ;
System.out.println(isValidDomain(str4));
String str5 = ".org" ;
System.out.println(isValidDomain(str5));
}
}
|
Python3
import re
def isValidDomain( str ):
regex = "^((?!-)[A-Za-z0-9-]" + "{1,63}(?<!-)\\.)" + "+[A-Za-z]{2,6}"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "geeksforgeeks.org"
print (isValidDomain(str1))
str2 = "write.geeksforgeeks.org"
print (isValidDomain(str2))
str3 = "-geeksforgeeks.org"
print (isValidDomain(str3))
str4 = "geeksforgeeks.o"
print (isValidDomain(str4))
str5 = ".org"
print (isValidDomain(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "geeksforgeeks.org" , "write.geeksforgeeks.org" , "-geeksforgeeks.org" , "geeksforgeeks.o" ,
".org" };
foreach ( string s in str) {
Console.WriteLine( isValidDomain(s) ? "true" : "false" );
}
Console.ReadKey(); }
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 );
}
}
|
Javascript
function isValidDomain(str) {
let regex = new RegExp(/^(?!-)[A-Za-z0-9-]+([\-\.]{1}[a-z0-9]+)*\.[A-Za-z]{2,6}$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "geeksforgeeks.org" ;
console.log(isValidDomain(str1));
let str2 = "contribute.geeksforgeeks.org" ;
console.log(isValidDomain(str2));
let str3 = "-geeksforgeeks.org" ;
console.log(isValidDomain(str3));
let str4 = "geeksforgeeks.o" ;
console.log(isValidDomain(str4));
let str5 = ".org" ;
console.log(isValidDomain(str5));
|
Output:
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)
Last Updated :
20 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...