URL Validation with Regex in Python

To validate URLs in Python using regular expressions, you can utilize the re module. Here’s an example of how you can validate a URL using regex:

import re

def validate_url(url):
    pattern = r'^(https?|ftp)://[^\s/$.?#].[^\s]*$'
    if re.match(pattern, url):
        return True
        return False

# Example usage
url1 = 'https://www.example.com'
url2 = 'ftp://example.com/file.txt'
url3 = 'invalid url'

print(validate_url(url1))  # True
print(validate_url(url2))  # True
print(validate_url(url3))  # False

In this example, the validate_url() function uses the regex pattern ^(https?|ftp)://[^\s/$.?#].[^\s]*$ to match URLs. Here’s a breakdown of the pattern:

  • ^ asserts the start of the string.
  • (https?|ftp) matches either “http”, “https”, or “ftp”.
  • :// matches the literal characters “://”.
  • [^\s/$.?#] matches any character except whitespace, “/”, “$”, “.”, “?”, and “#”.
  • . matches a single character.
  • [^\s]* matches zero or more characters except whitespace.
  • $ asserts the end of the string.

By using this pattern with re.match(), you can determine if a given string is a valid URL. The function returns True if the URL matches the pattern and False otherwise.

Please note that this regex pattern provides a basic validation for URLs based on common patterns but may not cover all possible URL variations. Depending on your specific requirements, you might need to adjust the regex pattern accordingly.