Python 正则表达式操作提取电子邮件 ID

Python regex manipulation extract email id

首先,我想从文本文件中抓取这种字符串

{kevin.knerr, sam.mcgettrick, mike.grahs}@google.com.au

然后转换成单独的字符串

kevin.knerr@google.com.au

sam.mcgettrick@google.com.au

mike.grahs@google.com.au

对于示例文本文件可以是:

一些乱码

{kevin.knerr, sam.mcgettrick, mike.grahs}@google.com.au

一些乱码

正如评论中所说,最好抓住 {} 中的部分,然后使用一些编程逻辑。您可以通过以下方式获取不同的部分:

\{(?P<individual>[^{}]+)\}@(?P<domain>\S+)
# looks for {
# captures everything not } into the group individual
# looks for @ afterwards
# saves everything not a whitespace into the group domain

a demo on regex101.com
Python 中,这将是:

import re
rx = r'\{(?P<individual>[^{}]+)\}@(?P<domain>\S+)'
string = 'gibberish {kevin.knerr, sam.mcgettrick, mike.grahs}@google.com.au gibberish'
for match in re.finditer(rx, string):
    print match.group('individual')
    print match.group('domain')

Python代码

ip = "{kevin.knerr, sam.mcgettrick, mike.grahs}@google.com.au"  
arr = re.match(r"\{([^\}]+)\}(\@\S+$)", ip)

#Using split for solution

for x in arr.group(1).split(","):
    print (x.strip() + arr.group(2))

#Regex Based solution

arr1 = re.findall(r"([^, ]+)", arr.group(1))
for x in arr1:
    print (x + arr.group(2))

IDEONE DEMO