Python Practice › RegEx Programs › Program 8
Extract URLs
Write a Python program to extract http and https URLs from text.
Output
['https://docs.python.org/3/', 'https://sqatools.in/python-tutorials/', 'http://example.com']
Approach
https?://\S+and trim trailing punctuation.
Solution
import re text = "Docs at https://docs.python.org/3/, tutorial https://sqatools.in/python-tutorials/. Old: http://example.com"print([u.rstrip(".,") for u in re.findall(r"https?://\S+", text)])Output
['https://docs.python.org/3/', 'https://sqatools.in/python-tutorials/', 'http://example.com']
Learn the concept
New to this topic? Read the lesson, then come back.