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 else: return False # Example usage url1 = ‘https://www.example.com’ url2 = ‘ftp://example.com/file.txt’ url3 = ‘invalid url’ print(validate_url(url1)) … Read more