How to Validate a URL Using Regular Expressions

Programming has made it easy to deal with structured and unstructured textual data. Tools like regular expressions and external libraries make these tasks a lot easier.

it’s possible to use most languages, including Python and JavaScript, to validate URLs using a regular expression. This example regex isn’t perfect, but you can use it to check URLs for simple use cases.

4

The regex to validate a URL presented in this article is not perfect. There may be multiple examples of valid URLs that may fail this regex validation. This includes URLs involving IP addresses, non-ASCII characters, and protocols like FTP. The following regex only validates the most common URLs.

Regular Expression to Validate a URL

The regex will consider a URL valid if it satisfies the following conditions:

you’re able to validate a URL in JavaScript using the following regular expression:

Spotify Daylist on an iPad with AirPods nearby

Similarly, you can use the following regex for URL validation in Python:

If you are uncomfortable with the above expression, check outa beginner’s guide to regular expressionsfirst. Regular expressions take some time to get used to. Exploring some examples likevalidating user account details using regular expressionsshould help.

A smart TV in a living room with a muted Mic appended on top

The above regex satisfies the following types of URLs:

Using the Regular Expression in a Program

The code used in this project is available in aGitHub repositoryand is free for you to use under the MIT license.

This is a Python approach to validating a URL:

BBC iPlayer showing on a smart TV.

This code uses Python’sre.compile()method to compile the regular expression pattern. This method accepts the regex pattern as a string parameter and returns a regex pattern object. This regex pattern object is further used to look for occurrences of the regex pattern inside the target string using there.search()method.

If it finds at least one match, there.search()method returns the first match. Note that if you want to search for all the matches to the pattern from the target string, you need to use there.findall()method.

HTML including a mention of a URL

Running the above code will confirm that the first URL is valid but the rest of them are not.

Similarly, you can validate a URL in JavaScript using the following code:

Again, running this code will confirm that the first URL is valid and the rest of them are invalid. It uses JavaScript’smatch()method to match the target string against a regular expression pattern.

Real-World Examples and Use Cases of URL Validation Using Regex

URL validation using regex can be crucial in several web development and data processing scenarios. Here are a few real-world examples and use cases:

Validate Important Data Using Regular Expressions

you may use regular expressions to search, match, or parse text. They are also used for natural language processing, pattern matching, and lexical analysis.

You can use this powerful tool to validate important types of data like credit card numbers, user account details, IP addresses, and more.

Regular expressions are the perfect tool to validate formats with simple, well-defined rules, like credit card numbers.

The best features aren’t the ones being advertised.

My iPhone does it all, but I still need my dumb phone.

Tor spoiled me forever.

The fix was buried in one tiny toggle.

Freeing up vital memory on Windows only takes a moment, and your computer will feel much faster once you’re done.

Technology Explained

PC & Mobile