URL Validation in PY
Match http and https URLs with optional www prefix, paths, query strings, and fragments.
Try it in the PY tester →Pattern
regexPY
https?:\/\/(?:www\.)?[\w\-]+(?:\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=%]* (flags: gi)Python (re) code
pyPython
import re
pattern = re.compile(r"https?:\/\/(?:www\.)?[\w\-]+(?:\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=%]*", re.IGNORECASE)
input_text = "https://www.example.com"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
Starts with http:// or https://, optional www., then one or more domain labels separated by dots, followed by any valid URL characters for the path and query.
Examples
Input
https://www.example.comMatches
https://www.example.com
Input
http://api.example.org/v1/users?id=42Matches
http://api.example.org/v1/users?id=42
Input
not a urlNo match
—