Extract URLs from text with regex in Python
Uses a regular expression to find and print HTTP/HTTPS URLs from a block of text.
Python code
14 linesimport re
text = """
Visit https://www.example.com for docs.
Contact support@mysite.org.
Check http://localhost:8000/api or ftp://files.example.net.
"""
url_pattern = r'https?://[^\s]+'
urls = re.findall(url_pattern, text)
for url in urls:
print(url)
Output
https://www.example.com
http://localhost:8000/api
How it works
The regex pattern https?://[^\s]+ matches the literal 'http', an optional 's', '://', then any non-whitespace characters. re.findall scans the entire text and returns every non-overlapping match as a list. The loop prints each URL on its own line. This pattern works for many common URLs but may include trailing punctuation.
Common mistakes
- Forgetting to import the `re` module
- Using a pattern that captures trailing punctuation like '.' or ')'
- Assuming the pattern matches ftp:// or other protocols
Variations
- Adjust pattern to `(?:https?|ftp)://[^\s]+` to include FTP URLs
- Use `re.finditer` to get match objects with positions in the text
Real-world use cases
- Scanning log files to extract external links for security analysis.
- Parsing user-generated content to detect and linkify URLs in a web app.
- Extracting API endpoints from configuration or documentation text.
Sponsored
More from Strings & text
- Automatically Detect Weak Passwords from Large Password Lists in Python easy
- Build CSV row from Python list with proper quoting easy
- Build a Secure Password Strength Checker in Python easy
- Convert Natural Language Dates to Datetime in Python medium
- Count Characters, Words, and Lines in Python Text easy
- Extract Data from Strings in Python: Beginner's Guide easy
Keep learning
Related tutorials and quizzes for this topic.