Check if an URL is valid or not using Regular Expression

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
Explaination :
The above URL is a valid URL.

Input : str = “https:// www.geeksforgeeks.org/”
Output : No
Explaination :
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 discuss in previous post.
Here the idea is to use Regular Expression to validate a URL.



    The following steps can be followed to compute the answer:

  1. Get the URL.
  2. Create a regular expression to check 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 and
    • then followed by :// and
    • then it must contain www. and
    • then followed by subdomain of length (2, 256) and
    • last part contains top level domain like .com, .org etc.
  3. Match the given URL with the regular expression. In Java, this can be done by using Pattern.matcher().
  4. Return true if the URL matches with the given regular expression, else return false.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Output:

Yes

Time Complexity : O (N)
Auxiliary Space : O (1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.