Validating URLs is a common task in web development. A regular expression can be used to check if a URL is well-formed. Here's a regular expression that can be used to validate a URL in Python:
p = re.compile('^(([^:/?#] ):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?')
The regular expression is broken down into the following parts:
Here's an example of how to use the regular expression to validate a URL:
import re
url = 'https://www.example.com/index.html'
p = re.compile('^(([^:/?#] ):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?')
m = p.match(url)
if m:
# The URL is valid.
print("Valid URL")
else:
# The URL is invalid.
print("Invalid URL")
This regular expression can validate most common URL formats, but it may need to be modified to accommodate specific requirements.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3