Skip to content

Instantly share code, notes, and snippets.

@embayer
Created February 10, 2015 14:36
Show Gist options
  • Select an option

  • Save embayer/a49f7303ae3e4f6a441c to your computer and use it in GitHub Desktop.

Select an option

Save embayer/a49f7303ae3e4f6a441c to your computer and use it in GitHub Desktop.
simple url validation
def validate_url(url):
"""
validates a homepage
string url: the url to validate
string return: the validate url or ""
"""
regex = re.compile(
r'^((?:http|http|ftp)s?://|www\.)' # http:// or https:// or ftp
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)
match = re.search(regex, url)
if match:
return url
else:
return ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment