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)
- How to check string is alphanumeric or not using Regular Expression
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Python - Check whether a string starts and ends with the same character or not (using Regular Expression)
- How to validate a domain name using Regular Expression
- How to validate CVV number using Regular Expression
- How to validate MAC address using Regular Expression
- How to validate HTML tag using Regular Expression
- How to validate IFSC Code using Regular Expression
- std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++
- How to validate MasterCard number using Regular Expression
- How to validate identifier using Regular Expression in Java
- Validating Roman Numerals Using Regular expression
- How to validate PAN Card number using Regular Expression
- How to validate pin code of India using Regular Expression
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- How to validate Visa Card number using Regular Expression
- How to validate time in 24-hour format using Regular Expression
- Find all the numbers in a string using regular expression in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.