linux

Using Regex To Extract Links.

Learn how to extract URLs from text using regular expressions in Python. Quick regex pattern for matching HTTP, HTTPS, and FTP links.

You can extract URLs from a block of text with a single regular expression.

(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+

This pattern matches HTTP, HTTPS, and FTP links. Here’s a Python script that uses it to pull URLs out of text:

python
text = "<CONTAINING URLS>"
urls = re.findall('(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+', text)
print(urls)