【发布时间】:2023-03-13 20:31:01
【问题描述】:
我想从一个 URL 中提取网站,即从以下 URL 中提取 console.aws.amazon.com。
>>> ts
'https://console.aws.amazon.com/ec2/home?region=us-east-1#s=Instances,EC2 Management Console,12/3/2012 4:34:57 PM,11,0,,25806'
>>> re.match(ts,'(")?http(s)?://(.*?)/').group(0)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
re.match(ts,'(")?http(s)?://(.*?)/').group(0)
AttributeError: 'NoneType' object has no attribute 'group'
我tried this regular expression in JS 成功了。知道为什么这在 JS 中匹配,但在 Python 中不起作用?
【问题讨论】:
-
Regex 或 regexp 如果你喜欢,但不是 regex。 Reg 普通 Ex 压力的缩写。
-
投票重新打开 - 因为这个特定问题要求使用正则表达式来提取域。答案下方的评论阐明了为什么 urlparse 不理想在这种情况下 - 即将导出一个 exe,并且包含的越少越好。