MST

星途 面试题库

面试题:Python正则表达式提取URL中的域名

给定一个URL,如'https://www.example.com/path/to/page?query=value',使用Python的正则表达式来提取其中的域名部分(即'www.example.com'),请写出相应代码。
30.2万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
import re

url = 'https://www.example.com/path/to/page?query=value'
match = re.search(r'https?://([^/]+)', url)
if match:
    domain = match.group(1)
    print(domain)