PYTHON

Validate URL Format with a Regular Expression

A Python regex pattern to validate common URL formats, including HTTP/HTTPS protocols, domain names, optional ports, paths, queries, and fragments, useful for data validation.

import re

def is_valid_url(url):
    # Basic URL regex: supports http(s), domain, optional port, path, query, fragment
    url_regex = re.compile(
        r'^(https?://)?'  # Protocol (http, https)
        r'((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*[a-zA-Z]{2,6})'  # Domain name
        r'(:\d+)?'  # Optional port
        r'(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#=~_\-]+))*$' # Path, query, fragment
    )
    return re.match(url_regex, url) is not None

print(is_valid_url("http://www.example.com")) # True
print(is_valid_url("https://example.com/path?query=1#fragment")) # True
print(is_valid_url("ftp://example.com")) # False
How it works: This Python function defines a regular expression to validate if a string is a well-formed URL. It checks for an optional `http://` or `https://` prefix, a valid domain name, an optional port number, and allows for paths, query parameters, and URL fragments. This is valuable for backend data processing and validation.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs