python正则表达式获取电子邮件地址的第一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15210485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python regex get first part of an email address
提问by JasonB
I am quite new to python and regex and I was wondering how to extract the first part of an email address upto the domain name. So for example if:
我对 python 和 regex 很陌生,我想知道如何将电子邮件地址的第一部分提取到域名。例如,如果:
s='[email protected]'
I would like the regex result to be (taking into account all "sorts" of email ids i.e including numbers etc..):
我希望正则表达式结果是(考虑到所有“种类”的电子邮件 ID,即包括数字等):
xjhgjg876896
I get the idea of regex - as in I know I need to scan till "@" and then store the result - but I am unsure how to implement this in python.
我得到了正则表达式的想法 - 因为我知道我需要扫描到“@”然后存储结果 - 但我不确定如何在 python 中实现它。
Thanks for your time.
谢谢你的时间。
采纳答案by David Robinson
回答by Tuxdude
As others have pointed out, the better solution is to use split.
正如其他人指出的那样,更好的解决方案是使用split.
If you're really keen on using regexthen this should work:
如果您真的很想使用,regex那么这应该有效:
import re
regexStr = r'^([^@]+)@[^@]+$'
emailStr = '[email protected]'
matchobj = re.search(regexStr, emailStr)
if not matchobj is None:
print matchobj.group(1)
else:
print "Did not match"
and it prints out
它打印出来
foo
NOTE:This is going to work only with email strings of [email protected]. If you want to match emails of type NAME<[email protected]>, you need to adjust the regex.
注意:这仅适用于[email protected]. 如果要匹配类型的电子邮件,NAME<[email protected]>则需要调整正则表达式。
回答by shantanoo
Few months back wrote EmailExtractor.py. You may like to try it out and modify it for your need. It extracts email address. You can split the output with '@' (recommended) or modify the regex.
几个月前写了EmailExtractor.py。您可能想尝试一下并根据需要对其进行修改。它提取电子邮件地址。您可以使用“@”(推荐)拆分输出或修改正则表达式。
回答by Pavan G jakati
Below should help you do it :
下面应该可以帮助你做到这一点:
fromAddr = message.get('From').split('@')[1].rstrip('>')
fromAddr = fromAddr.split(' ')[0]
回答by Sindri Tór
Good answers have already been answered but i want to put mine anyways.
好的答案已经得到了回答,但我还是想放我的。
If i have an email [email protected] i want to get just "john".
i want to get only "john"
If i have an email [email protected] i want to get just "john"
i want to get only "john"
如果我有电子邮件 [email protected],我只想得到“john”。
我只想得到“约翰”
如果我有电子邮件 [email protected] 我只想得到“john”
我只想得到“约翰”
so this is what i did:
所以这就是我所做的:
name = recipient.split("@")[0]
name = name.split(".")[0]
print name
cheers
干杯
回答by Rizqi N. Assyaufi
#!/usr/bin/python3.6
def email_splitter(email):
username = email.split('@')[0]
domain = email.split('@')[1]
domain_name = domain.split('.')[0]
domain_type = domain.split('.')[1]
print('Username : ', username)
print('Domain : ', domain_name)
print('Type : ', domain_type)
email_splitter('[email protected]')
Output :
输出 :
Username : foo.goo
Domain : bar
Type : com
回答by Eda
You can also try to use email_split.
您也可以尝试使用 email_split。
from email_split import email_split
email = email_split('[email protected]')
email.local # xjhgjg876896
email.domain # domain.com
You can find more here https://pypi.org/project/email_split/. Good luck :)
你可以在这里找到更多https://pypi.org/project/email_split/。祝你好运 :)
回答by jhrr
You shouldn't use a regex or split.
您不应该使用正则表达式或split.
local, at, domain = '[email protected]'.rpartition('@')
回答by Stryker
Here is another way, using the index method.
这是另一种方法,使用索引方法。
s='[email protected]'
# Now lets find the location of the "@" sign
index = s.index("@")
# Next lets get the string starting from the begining up to the location of the "@" sign.
s_id = s[:index]
print(s_id)
And the output is
输出是
xjhgjg876896

