要提取url的域名部分,可以使用正则表达式来进行匹配。以下是一个示例代码,演示如何使用正则表达式提取URL的域名部分:
python
import re
# 正则表达式模式,匹配URL的域名部分
pattern = r'(?:http[s]?://)?([^/]+)'
# 示例URL
url = 'http://www.example.com/path/page.html'
# 使用正则表达式匹配域名部分
match = re.match(pattern, url)
# 提取匹配到的域名部分
domain = match.group(1)
print(domain) # 输出结果:www.example.com
这个示例中,使用了正则表达式模式 `(?:http[s]?://)?([^/]+)` 来匹配URL的域名部分。其中 `(?:http[s]?://)?` 匹配 URL 中的 `http://` 或 `https://` 部分(可选),`[^/]+` 匹配 URL 中的域名部分。
查看详情
查看详情