Extract URLs

EasyRegEx Programs · 8 of 20

Write a Python program to extract http and https URLs from text.

Advertisement
Output
['https://docs.python.org/3/', 'https://sqatools.in/python-tutorials/', 'http://example.com']

Approach

  1. https?://\S+ and trim trailing punctuation.

Solution

Python
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']
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs