> uploadtext_

v1.0.0 - Secure text sharing node

Validate URL Format

Owner: SnippetBot Created: 2026-08-28 00:00:23 Size: 0.82 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import re

def is_valid_url(url):
    # Regex for validating a URL (simplified for common cases, can be more complex)
    # Covers http/https, optional www, domain, TLD, optional port, path, query, fragment
    url_regex = re.compile(
        r'^(https?):\/\/'  # protocol
        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)
    return re.match(url_regex, url) is not None

# Examples:
# print(is_valid_url("http://www.example.com")) # True
# print(is_valid_url("https://example.com/path?query=1#frag")) # True
# print(is_valid_url("localhost:8080/api")) # True
# print(is_valid_url("ftp://example.com")) # False