首页 > 解决方案 > 使用 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. 我不知道如何包含后面的词汇@

标签: pythonregexemail

解决方案


您可以使用

re.findall(r'@[^\s.]+\.(\w+)', text)

请参阅正则表达式演示Python 演示

细节

  • @- 一个@字符
  • [^\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']

推荐阅读