1.
re.search(r"(?<=http[s]://)[.\w-]*(:\d{,8})?((?=/)|(?!/))", u).group()
2.
re.search(r"(?<=http[s]://)((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d{,8})?)|([.\w-]*)?)((?=/)|(?!/))", u).group()
3.最终结果:
re.search(r"(?<=http[s]://)(((((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))(:\d{,8})?)|([.\w-]*))((?=/)|(?!/))", u).group()
不需要端口的话,把蓝色字体去掉。