Wednesday, August 9, 2023

Q9) Write a python program to validate the URL by using regular expression.

import re

 

def validate_url(url):

    pattern = r'^(http|https)://([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,6})(/[a-zA-Z0-9.-]*)*([?][a-zA-Z0-9.-]*)*$'

    if re.match(pattern, url):

        return True

    else:

        return False

 

# Test the program

url_to_validate = input("Enter a URL: ")

 

if validate_url(url_to_validate):

    print("Valid URL")

else:

    print("Invalid URL")

No comments:

Post a Comment