Regex: Python Vs. Sed Replace The Fqdn Of A Url But Preserve Port
TL;DR: I'm trying to change the FQDN of a URL but keep the port using python's re.sub. Example Input: http://www.yahoo.com:80/news.html http://new.news.com/news.html https://www
Solution 1:
But if you are using Python 2 or just want to use a Regex:
import re
url = 'http://www.yahoo.com:80/news.html'
new_url = re.sub(r'(?<=://)(.*?)(?=[:/])', 'www.google.com', url)
print new_url
Solution 2:
You need to use the right tool for the right job. urlparse
is that tool.
from urllib.parse import urlparse #python 3
url = 'http://www.yahoo.com:80/news.html'
url = urlparse(url)
url._replace(netloc="{}:{}".format('www.google.com', url.port))
print url.geturl()
Post a Comment for "Regex: Python Vs. Sed Replace The Fqdn Of A Url But Preserve Port"