Check if an URL is valid or not using Regular Expression Last Updated : 07 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Given a URL as a character string str of size N.The task is to check if the given URL is valid or not.Examples : Input : str = "https://www.geeksforgeeks.org/" Output : Yes Explanation : The above URL is a valid URL.Input : str = "https:// www.geeksforgeeks.org/" Output : No Explanation : Note that there is a space after https://, hence the URL is invalid. Approach : An approach using java.net.url class to validate a URL is discussed in the previous post. Here the idea is to use Regular Expression to validate a URL. Get the URL.Create a regular expression to check the valid URL as mentioned below: regex = "((http|https)://)(www.)?" + "[a-zA-Z0-9@:%._\\+~#?&//=]{2,256}\\.[a-z]" + "{2,6}\\b([-a-zA-Z0-9@:%._\\+~#?&//=]*)" The URL must start with either http or https andthen followed by :// andthen it must contain www. andthen followed by subdomain of length (2, 256) andlast part contains top level domain like .com, .org etc.Match the given URL with the regular expression. In Java, this can be done by using Pattern.matcher().Return true if the URL matches with the given regular expression, else return false. Below is the implementation of the above approach: C++ // C++ program to validate URL // using Regular Expression #include <iostream> #include <regex> using namespace std; // Function to validate URL // using regular expression bool isValidURL(string url) { // Regex to check valid URL const regex pattern("((http|https)://)(www.)?[a-zA-Z0-9@:%._\\+~#?&//=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%._\\+~#?&//=]*)"); // If the URL // is empty return false if (url.empty()) { return false; } // Return true if the URL // matched the ReGex if(regex_match(url, pattern)) { return true; } else { return false; } } // Driver Code int main() { string url = "https://www.geeksforgeeks.org"; if (isValidURL(url)) { cout << "YES"; } else { cout << "NO"; } return 0; } // This code is contributed by yuvraj_chandra Java // Java program to check URL is valid or not // using Regular Expression import java.util.regex.*; class GFG { // Function to validate URL // using regular expression public static boolean isValidURL(String url) { // Regex to check valid URL String regex = "((http|https)://)(www.)?" + "[a-zA-Z0-9@:%._\\+~#?&//=]" + "{2,256}\\.[a-z]" + "{2,6}\\b([-a-zA-Z0-9@:%" + "._\\+~#?&//=]*)"; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the string is empty // return false if (url == null) { return false; } // Find match between given string // and regular expression // using Pattern.matcher() Matcher m = p.matcher(url); // Return if the string // matched the ReGex return m.matches(); } // Driver code public static void main(String args[]) { String url = "https://www.geeksforgeeks.org"; if (isValidURL(url) == true) { System.out.println("Yes"); } else System.out.println("NO"); } } Python3 # Python3 program to check # URL is valid or not # using regular expression import re # Function to validate URL # using regular expression def isValidURL(str): # Regex to check valid URL regex = ("((http|https)://)(www.)?" + "[a-zA-Z0-9@:%._\\+~#?&//=]" + "{2,256}\\.[a-z]" + "{2,6}\\b([-a-zA-Z0-9@:%" + "._\\+~#?&//=]*)") # 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: url = "https://www.geeksforgeeks.org" if(isValidURL(url) == True): print("Yes") else: print("No") # This code is contributed by avanitrachhadiya2155 C# // C# program to check URL is valid or not //using Regular Expressions using System; using System.Text.RegularExpressions; class GFG { // Main Method static void Main(string[] args) { // Input strings to Match // Valid URL string[] str={"https://www.geeksforgeeks.org"}; foreach(string s in str) { Console.WriteLine( isValidURL(s) ? "true" : "false"); } Console.ReadKey(); } // method containing the regex public static bool isValidURL(string str) { string strRegex = @"((http|https)://)(www.)?" + "[a-zA-Z0-9@:%._\\+~#?&//=]" + "{2,256}\\.[a-z]" + "{2,6}\\b([-a-zA-Z0-9@:%" + "._\\+~#?&//=]*)"; Regex re = new Regex(strRegex); if (re.IsMatch(str)) return (true); else return (false); } } // This code is contributed by Rahul Chauhan JavaScript function isValidURL(str) { if(/^(http(s):\/\/.)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)$/g.test(str)) { console.log('YES'); } else { console.log('NO'); } } isValidURL("https://www.geeksforgeeks.org"); // This code is contributed by Rahul Chauhan Output: Yes Time Complexity: O (N) Auxiliary Space: O (1) Comment More infoAdvertise with us Next Article Check if an URL is valid or not using Regular Expression P prashant_srivastava Follow Improve Article Tags : Strings Pattern Searching DSA python-regex java-regular-expression CPP-regex Java-URL regular-expression +4 More Practice Tags : Pattern SearchingStrings Similar Reads How to check Aadhaar number is valid or not using Regular Expression Given string str, the task is to check whether the given string is a valid Aadhaar number or not by using Regular Expression. The valid Aadhaar number must satisfy the following conditions: It should have 12 digits.It should not start with 0 and 1.It should not contain any alphabet and special chara 5 min read How to validate ISIN using Regular Expressions ISIN stands for International Securities Identification Number. Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must sati 6 min read Validating UPI IDs using Regular Expressions Given some UPI IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid UPI ID: UPI ID is an alphanumeric String i.e., formed using digits(0-9), alphabets (A-Z and a-z), and other special characters.It must contain '@'.It should not contain whitespace.It may 5 min read How to validate pin code of India using Regular Expression Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression. The valid pin code of India must satisfy the following conditions. It can be only six digits.It should not start with zero.First digit of the pin cod 6 min read How to validate a Username using Regular Expressions in Java Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or 3 min read Like