python - python中的正则表达式,需要从字符串中打印网站名称
问题描述
import re
x = 'my website name is www.algoexpert.com and i have other website too'
for line in x:
y = line.rstrip()
z = re.findall('.*\S+/.[a-z]{0-9}/.\S+', y)
print(z)
我只想打印网站名称 ( www.algoexpert.com
)
解决方案
要修复的问题:
x
本身就是一个字符串,你为什么要用它来循环它for line in x
?[a-z]{0-9}
- 尝试仅覆盖字母字符,尽管方式错误(可能是{0,9}
)。字符的范围应该[a-z0-9]+
或至少 -[a-z]+
(取决于最初的意图)- 点/句点
.
应该用反斜杠转义\.
固定版本(简化):
import re
x = 'my website name is www.algoexpert.com and i have other website too'
z = re.findall('\S+\.[a-z0-9]+\.\S+', x.strip())
print(z) # ['www.algoexpert.com']
推荐阅读
- sql-server - 将 int 转换为 hh:mm 格式
- asp.net - ASP.NET 在 EF 数据库中存储时间跨度
- gremlin - Gremlin - 如何在获得最短路径时过滤属性边缘
- oracle - oracle 表中的 CURRENT_TIMESTAMP
- c# - 以不同用户身份从备份恢复 SQL 数据库
- excel - 使用 VBA 将 ListColumn 从一个表复制到另一个表时没有错误也没有数据
- python - Pyinstaller - 错误消息:名称 Actor 未定义
- android - 如何在我的项目结构中设置位置?
- java - 如何在 mockito 中模拟地图?
- angular - 如果列表为空,如何禁用 mat-form-field 或 mat-autocomplete?