Given a URL as a character string str of size N.The task is to check if the given URL is valid or not.
Input : str = “https://www.geeksforgeeks.org/”
Output : Yes
The above URL is a valid URL.
Input : str = “https:// www.geeksforgeeks.org/”
Output : No
Note that there is a space after https://, hence the URL is invalid.
- 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.)?”
- 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:
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.
- 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)
- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.