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:
- Get the URL.
- 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.
- 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:
// 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 if (isValidURL(url) == true) { System.out.println("Yes"); } else System.out.println("NO"); } } |
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.
Recommended Posts:
- How to check Aadhar number is valid or not using Regular Expression
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Python - Check whether a string starts and ends with the same character or not (using Regular Expression)
- How to check string is alphanumeric or not using Regular Expression
- Check if URL is valid or not in Java
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- How to validate MAC address using Regular Expression
- How to validate Indian driving license number using Regular Expression
- How to validate CVV number using Regular Expression
- Find all the numbers in a string using regular expression in Python
- How to validate SSN (Social Security Number) using Regular Expression
- Remove duplicate words from Sentence using Regular Expression
- How to validate identifier using Regular Expression in Java
- How to validate time in 12-hour format using Regular Expression
- How to validate PAN Card number using Regular Expression
- Validating Roman Numerals Using Regular expression
- How to validate time in 24-hour format using Regular Expression
- How to validate pin code of India using Regular Expression
- How to validate Hexadecimal Color Code using Regular Expression
- How to validate image file extension using Regular Expression
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.

