Python 正则表达式匹配二级域名
以下是匹配二级域名的正则表达式:/n/npython/nimport re/n/nurl = 'https://www.example.com'/npattern = re.compile(r'https?://([/w-]+/.)*([/w-]+/.[/w-]+)')/nmatch = pattern.search(url)/nif match:/n print(match.group(2)) # 输出 'example.com'/n/n/n解释一下正则表达式:/n/n- 'https?://' 匹配 'http://' 或 'https://'/n- '([/w-]+/.)*' 匹配任意个由字母、数字或 '-' 组成的子域名,后面跟一个 '.'/n- '([/w-]+/.[/w-]+)' 匹配主域名,由字母、数字或 '-' 组成/n/n这个正则表达式还可以匹配多级子域名,例如 'www.blog.example.com'。
原文地址: https://www.cveoy.top/t/topic/mBox 著作权归作者所有。请勿转载和采集!