python - 忽略 .com 和 .org 或 .net 的正则表达式解决方案
问题描述
我有以下字符串
str1="Google.com"
str2="yahoo.com"
str3="redcross.org"
我的问题是,忽略 .com、.org 或 .net 的有效正则表达式解决方案是什么
预期产出
Google
yahoo
redcross
解决方案
尝试:
import re # Standard regex module
# The ReGeX
regex = re.compile('([\\.a-zA-Z0-9-]+)(?=\\.[a-z]{3,5})')
# The document to extract websites (suffix excluded) from
doc = """
str1="Google.com"
str2="yahoo.com"
str3="redcross.org"
"""
# Find websites (without the suffix) like so:
found_websites = regex.findall(doc)
# Confirm by printing
print(found_websites)
输出:
['Google', 'yahoo', 'redcross']
功能证明:证明
编辑:我在这里做了一个信息更丰富的网站查找器(不是你想要的,我认为,但可能认为有用)
推荐阅读
- javascript - 如何在 React.js 中正确进行条件渲染?
- flutter - 颤动@error:配置项目':google_sign_in'时出现问题
- pytorch - 如何在训练 CNN 时辨别哪张图像生成了特定的特征图?
- python - 过滤使用 generate_series 的查询同时保留空行
- linux - Linux 如何测量特定进程的经过时间?
- python - 从工作区中的 Jupyter Notebook 运行 Azure Auto-ML 实验时,假日国家/地区设置不起作用
- c# - 文档提取认知技能的使用
- asp.net-core - ASP.NET Core MVC 中的数字字段验证错误
- javascript - 无法在控制台中显示输入值
- pine-script - 如何隐藏绘图输入复选框