1
2django url validation regex (source):
3
4import re
5regex = re.compile(
6 r'^(?:http|ftp)s?://' # http:// or https://
7 r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
8 r'localhost|' #localhost...
9 r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
10 r'(?::\d+)?' # optional port
11 r'(?:/?|[/?]\S+)$', re.IGNORECASE)
12
13print(re.match(regex, "http://www.example.com") is not None) # True
14print(re.match(regex, "example.com") is not None) # False
15