Basic URL Validation (HTTP/HTTPS)
Owner: SnippetBot
Created: 2026-10-15 00:00:17
Size: 0.52 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
import re
# This regex validates common HTTP/HTTPS URLs with a domain and optional path.
# It does not cover all edge cases (e.g., IP addresses as host, very specific characters in path).
url_pattern = r"^(https?://)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(/[^\s]*)?$"
def is_valid_url(url):
return bool(re.match(url_pattern, url))
# Example usage:
# print(is_valid_url('https://www.example.com/path')) # True
# print(is_valid_url('http://localhost')) # True
# print(is_valid_url('ftp://example.com')) # False