python regex get first part of an email address
Question:
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:
s='[email protected]'
I would like the regex result to be (taking into account all “sorts” of email ids i.e including numbers etc..):
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.
Thanks for your time.
Answers:
You should just use the split
method of strings:
s.split("@")[0]
As others have pointed out, the better solution is to use split
.
If you’re really keen on using regex
then this should work:
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.
Below should help you do it :
fromAddr = message.get('From').split('@')[1].rstrip('>')
fromAddr = fromAddr.split(' ')[0]
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”
so this is what i did:
name = recipient.split("@")[0]
name = name.split(".")[0]
print name
cheers
#!/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
You can also try to use 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 🙂
You shouldn’t use a regex or split
.
local, at, domain = '[email protected]'.rpartition('@')
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
need to install package
pip install email_split
from email_split import email_split
email = email_split("[email protected]")
print(email.domain)
print(email.local)
You can find all the words in the email and then return the first word.
import re
def returnUserName(email):
return re.findall("w*",email)[0]
print(returnUserName("[email protected]")) #Output is - johns123
print(returnUserName('[email protected]')) #Output is - xjhgjg876896
The following will return the continuous text before @
re.findall(r'(S+)@', s)
You have to use right RFC5322 parser.
"@@@@@"@example.com
is a valid email address, and semantically localpart("@@@@@"
) is different from its username(@@@@@
)
As of python3.6, you can use email.headerregistry
:
from email.headerregistry import Address
s='[email protected]'
Address(addr_spec=s).username # => 'xjhgjg876896'
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:
s='[email protected]'
I would like the regex result to be (taking into account all “sorts” of email ids i.e including numbers etc..):
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.
Thanks for your time.
You should just use the split
method of strings:
s.split("@")[0]
As others have pointed out, the better solution is to use split
.
If you’re really keen on using regex
then this should work:
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.
Below should help you do it :
fromAddr = message.get('From').split('@')[1].rstrip('>')
fromAddr = fromAddr.split(' ')[0]
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”
so this is what i did:
name = recipient.split("@")[0]
name = name.split(".")[0]
print name
cheers
#!/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
You can also try to use 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 🙂
You shouldn’t use a regex or split
.
local, at, domain = '[email protected]'.rpartition('@')
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
need to install package
pip install email_split
from email_split import email_split
email = email_split("[email protected]")
print(email.domain)
print(email.local)
You can find all the words in the email and then return the first word.
import re
def returnUserName(email):
return re.findall("w*",email)[0]
print(returnUserName("[email protected]")) #Output is - johns123
print(returnUserName('[email protected]')) #Output is - xjhgjg876896
The following will return the continuous text before @
re.findall(r'(S+)@', s)
You have to use right RFC5322 parser.
"@@@@@"@example.com
is a valid email address, and semantically localpart("@@@@@"
) is different from its username(@@@@@
)
As of python3.6, you can use email.headerregistry
:
from email.headerregistry import Address
s='[email protected]'
Address(addr_spec=s).username # => 'xjhgjg876896'