I found a lot of questions for this, but I found no practical answer.
I don't need http or www, users should be allowed to enter e.g. example.com
Allowed:
- http://www.example.berlin
- https://example.de/more
- www.sub.example.de
- example.de
Not allowed:
- example
- 1
- :::::
- .....
problem with other questions:
Python - How to validate a url in python ? (Malformed or not) (does not accept "example.com")
http://validators.readthedocs.io/en/latest/ (does not accept "example.com")
https://stackoverflow.com/a/2532344/1907997 (allows "example")
^([a-z0-9]+(-[a-z0-9]+)*.)+[a-z]{2,}$ (does not allow http or sub domains)
- urlparse (does not accept "example.com")
So I think I need regular expression for this like in the last example above but a little bit extended. https://docs.python.org/3.6/library/re.html
With this code all examples are working but not "example.de/more"
def verify_url(self, url):
url = url.strip()
if url[-1] == ".":
url = url[:-1]
if url[-1] == "/":
url = url[:-1]
url = url.replace("https://", "")
url = url.replace("http://", "")
if url.startswith("www."):
url = url.replace("www.", "")
result = re.match(
"^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$",
url)
if result:
return url