PYTHON

Validate URL Format in Python

A Python regex pattern to validate common URL formats, including HTTP/HTTPS protocols and various domain structures, useful for backend data processing.

import re 

url_regex = re.compile( 
    r'^(?:http|ftp)s?://'  # http:// or https:// 
    r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'  # domain... 
    r'localhost|'  # localhost... 
    r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'  # ...or ip 
    r'(?::\d+)?'  # optional port 
    r'(?:/?|[/?]\S+)$', re.IGNORECASE) 

def is_valid_url(url): 
    return bool(url_regex.match(url)) 

# Examples: 
# print(is_valid_url("https://www.google.com/search?q=regex")) # True 
# print(is_valid_url("ftp://example.org")) # True 
# print(is_valid_url("not-a-url")) # False
How it works: This Python snippet uses the `re` module to compile a regular expression (`url_regex`) designed to validate URLs. It supports `http`, `https`, `ftp`, and `ftps` protocols, common domain name structures, IP addresses, and optional port numbers, along with paths and query parameters. The `is_valid_url` function returns `True` if the string matches the pattern, `False` otherwise.

Need help integrating this into your project?

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

Hire DigitalCodeLabs