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 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:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValidURL(string url)
{
const regex pattern( "((http|https)://)(www.)?[a-zA-Z0-9@:%._\\+~#?&//=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%._\\+~#?&//=]*)" );
if (url.empty())
{
return false ;
}
if (regex_match(url, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
if (isValidURL(url))
{
cout << "YES" ;
}
else
{
cout << "NO" ;
}
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean
isValidURL(String url)
{
String regex = "((http|https)://)(www.)?"
+ "[a-zA-Z0-9@:%._\\+~#?&//=]"
+ "{2,256}\\.[a-z]"
+ "{2,6}\\b([-a-zA-Z0-9@:%"
+ "._\\+~#?&//=]*)" ;
Pattern p = Pattern.compile(regex);
if (url == null ) {
return false ;
}
Matcher m = p.matcher(url);
return m.matches();
}
public static void main(String args[])
{
String url
if (isValidURL(url) == true ) {
System.out.println( "Yes" );
}
else
System.out.println( "NO" );
}
}
|
Python3
import re
def isValidURL( str ):
regex = ( "((http|https)://)(www.)?" +
"[a-zA-Z0-9@:%._\\+~#?&//=]" +
"{2,256}\\.[a-z]" +
"{2,6}\\b([-a-zA-Z0-9@:%" +
"._\\+~#?&//=]*)" )
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
if (isValidURL(url) = = True ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
foreach ( string s in str) {
Console.WriteLine( isValidURL(s) ? "true" : "false" );
}
Console.ReadKey(); }
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 );
}
}
|
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' );
}
}
|
Time Complexity: O (N)
Auxiliary Space: O (1)