python - 如何匹配网址但不包含点
问题描述
我正在尝试使用以下代码对部分 URL 进行分组和匹配:
pattern = '(http|https\:\/\/)([a-zA-Z0-9\-\.]+\.)([a-zA-Z]{2,3})'
re.search(pattern, 'https://www.university.edu/').groups()
# what I got is ('https://', 'www.university.', 'edu')
# but what I expect is ('https://', 'www.university', 'edu')
如上所示,对于第二部分,目前我只能获取字符加 a .
,但是如何更改我的代码以使第二部分中没有点?
谢谢!
解决方案
import re
pattern = '(http|https:\/\/)([a-zA-Z0-9\-\.]+)\.([a-zA-Z]{2,3})'
print(re.search(pattern, 'https://www.university.edu/').groups())
推荐阅读
- django - Django“get_git_revision 失败:[Errno 2] 没有这样的文件或目录:'.../.git/HEAD'”
- opencl - 在 OpenCL 中进行标量向量乘法的最快方法
- sql - C# - 使用表别名时的 SQLite 错误:没有这样的列
- groovy - 读取 NiFi 系统属性不适用于 groovy
- javascript - React HOC:TypeError:无法将类作为函数调用
- python - Windows 上的 Flask Web 服务器如何使用 Windows 证书存储中的 SSL 证书?
- vba - 在表达式生成器中使用自定义函数
- python - 使用 Python 更正日期和时间
- javascript - 如何用里面的新对象更新我的数据库文档?
- javascript - 使用此脚本打印时如何保留元素输入?