首页 > 解决方案 > 如何匹配网址但不包含点

问题描述

我正在尝试使用以下代码对部分 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 .,但是如何更改我的代码以使第二部分中没有点?

谢谢!

标签: pythonpython-3.xregex

解决方案


import re
pattern = '(http|https:\/\/)([a-zA-Z0-9\-\.]+)\.([a-zA-Z]{2,3})'
print(re.search(pattern, 'https://www.university.edu/').groups())

推荐阅读