26 lines
766 B
Python
26 lines
766 B
Python
#!/usr/bin/env python3
|
|
"""Extract all URLs from text using regex."""
|
|
import re
|
|
import sys
|
|
|
|
def extract_urls(text: str) -> list[str]:
|
|
"""Extract HTTP/HTTPS URLs from text.
|
|
|
|
Returns a list of unique URLs preserving order of first appearance.
|
|
"""
|
|
url_pattern = r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+[^\s\'"<>)]*'
|
|
matches = re.findall(url_pattern, text)
|
|
seen = set()
|
|
result = []
|
|
for url in matches:
|
|
clean = url.rstrip('.,;:!?)')
|
|
if clean not in seen:
|
|
seen.add(clean)
|
|
result.append(clean)
|
|
return result
|
|
|
|
if __name__ == '__main__':
|
|
input_text = sys.stdin.read() if not sys.argv[1:] else ' '.join(sys.argv[1:])
|
|
urls = extract_urls(input_text)
|
|
for url in urls:
|
|
print(url) |