python re 正则 提取域名

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()

不需要端口的话,把蓝色字体去掉。

猜你喜欢

转载自blog.csdn.net/carrey_0612/article/details/88813836