python - 使用 Python 正则表达式提取部分电子邮件地址
问题描述
我有以下字符串:
'iamgood@yahoo.com, cc.a@est.gov.cn, j_193@ywuancjds.com, super.hard.bb@ililsa.bi'
我想提取com, gov, com, bi
.
我写了(?<=@)\w+
正则表达式,但结果是提取yahoo,est,ywuancjds,ililsa
. 我不知道如何包含后面的词汇@
。
解决方案
您可以使用
re.findall(r'@[^\s.]+\.(\w+)', text)
细节
@
- 一个@
字符[^\s.]+
- 除空格和点之外的 1 个或多个字符\.
- 一个点(\w+)
- 第 1 组(在该组中捕获的值只会由返回re.findall
):一个或多个单词字符。
Python 演示片段:
import re
text = "iamgood@yahoo.com, cc.a@est.gov.cn, j_193@ywuancjds.com, super.hard.bb@ililsa.bi"
print( re.findall(r"@[^\s.]+\.(\w+)", text) )
# => ['com', 'gov', 'com', 'bi']
推荐阅读
- python - 如何为熊猫 groupby 使用多个 lambda 函数
- c++ - 日志/线性 Z-Buffer 阴影贴图
- python - socket.timeout : 读操作超时?Gmail API
- excel - 如何为在 Windows 上运行的 Excel 创建 docker 文件?
- python - Skimage.io.imread 读取 2D 数组而不是 3D
- javascript - 运行 react-native 应用程序时出现此错误“index.js:Transformer is not a constructor”
- tensorflow - 使用 ONNX 模型 Tensorflow 在 ML.NET 中获取标签和分数预测
- node.js - AWS Lambda 自定义 Nodejs 容器显示运行时错误
- javascript - 如何从 contenteditable 标记中选择并突出显示外文本
- typescript - 打字稿:为什么A扩展B和B扩展A并不意味着A等于B?