

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you validate a URL with a regular expression in Python?
There's no validate method as almost anything is a valid URL. There are some punctuation rules for splitting it up. Without any punctuation, you still have a valid URL.
Depending on the situation, we use following methods.
If you trust the data, and just want to verify if the protocol is HTTP, then urlparse is perfect.
If you want to make the URL is actually a true URL, use the cumbersome and maniacal regex
If you want to make sure it's a real web address, use the following code
Example
import urllib try: urllib.urlopen(url) except IOError: print "Not a real URL"
- Related Questions & Answers
- How to validate a URL using regular expression in C#?
- How to write a Python Regular Expression to validate numbers?
- How do you access the matched groups in a JavaScript regular expression?
- How to validate an email id using regular expression in Python?
- Can you explain Python Regular Expression Syntax in a simple way?
- How do we use Python regular expression to match a date string?
- How do we use a delimiter to split string in Python regular expression?
- Regular Expression in Python with Examples?
- How do we use Python Regular Expression named groups?
- Validate URL in ReactJS
- How do we use re.finditer() method in Python regular expression?
- How do I clear the regular expression cache in Python?
- What is a regular expression in Python?
- How do you append to a file with Python?
- How to use Python Regular expression to extract URL from an HTML link?
Advertisements