21

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='xjhgjg876896@domain.com'

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.

JasonB
  • 555
  • 2
  • 7
  • 13
  • Do you _need_ to use regex for this (e.g., as part of a homework assignment or something)? Or are you just guessing that there's no other way to do this? – abarnert Mar 04 '13 at 20:31
  • 1
    If you _do_ need to use a regex, you will have to read a tutorial on them, and on the Python `re` module. If I just said "Use `re.match('^(.*?)@', s)`", you wouldn't know how to use the thing that comes back, how to debug or extend it, etc., so what you be the point? – abarnert Mar 04 '13 at 20:34
  • Do you also want to parse these valid email addresses: `Tony Snow ` and `(tony snow) tony@example.com`? What do you want to return from `tony%example.com@example.org` ? The current standard for the format of an email address is here: http://www.rfc-editor.org/rfc/rfc5322.txt – Robᵩ Mar 04 '13 at 20:36
  • If you need to parse complete email addresses, not just this simple form, you even more definitely don't want a regex. See [`email.utils.parseaddr`](http://docs.python.org/2/library/email.util.html#email.utils.parseaddr) in the std lib and friends, or search for third-party libraries on PyPI if for some reason this isn't appropriate. Getting all of the details right is very hard. And this is exactly why python comes with batteries included. – abarnert Mar 04 '13 at 20:51

14 Answers14

69

You should just use the split method of strings:

s.split("@")[0]
abarnert
  • 354,177
  • 51
  • 601
  • 671
David Robinson
  • 77,383
  • 16
  • 167
  • 187
4

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 = 'foo@bar.baz'
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 SOMEONE@SOMETHING.TLD. If you want to match emails of type NAME<SOMEONE@SOMETHING.TLD>, you need to adjust the regex.

Sindri Þór
  • 2,887
  • 3
  • 26
  • 32
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
3

You shouldn't use a regex or split.

local, at, domain = 'john.smith@example.org'.rpartition('@')
jhrr
  • 1,624
  • 14
  • 22
  • The question was only to get the first part of the email address. So it would be better ``prefix, _, _ = 'john.smith@example.org'.rpartition('@')``. – ikreb Feb 09 '23 at 14:49
3

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='xjhgjg876896@domain.com'
Address(addr_spec=s).username # => 'xjhgjg876896'
ernix
  • 3,442
  • 1
  • 17
  • 23
1
#!/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('foo.goo@bar.com')

Output :

Username :  foo.goo
Domain   :  bar
Type     :  com
1

Here is another way, using the index method.

s='xjhgjg876896@domain.com'

# 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
Stryker
  • 5,732
  • 1
  • 57
  • 70
1

need to install package pip install email_split

from email_split import email_split
email = email_split("ssss@ggh.com")
print(email.domain)
print(email.local)
0

Below should help you do it :

 fromAddr = message.get('From').split('@')[1].rstrip('>')
        fromAddr = fromAddr.split(' ')[0]
Pavan G jakati
  • 115
  • 1
  • 7
0

Good answers have already been answered but i want to put mine anyways.

  • If i have an email john@gmail.com i want to get just "john".

    i want to get only "john"

  • If i have an email john.joe@gmail.com 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

Sindri Þór
  • 2,887
  • 3
  • 26
  • 32
0

You can also try to use email_split.

from email_split import email_split
email = email_split('xjhgjg876896@domain.com')
email.local  # xjhgjg876896
email.domain  # domain.com

You can find more here https://pypi.org/project/email_split/ . Good luck :)

Eda
  • 1
  • 1
0

The following will return the continuous text before @

 re.findall(r'(\S+)@', s)
0

split() is an elegant option, but if asked to use REGEX, utilizing grouping to capture the part before @ is quite straightforward.

the example below captures the name part which contains a-z,A-Z,0-9 only. Grouping is achieved by using ( ) around that part of regex, i.e. ([a-zA-Z0-9]+).

import re
regex_email = r"([a-zA-Z0-9]+)@\S+"
s1 = "This is my email: hello@world.com"
print(re.findall(regex_email, s1)) 

#output is ['hello']
MiroG
  • 1
  • 1
0

Hope this will help:

def get_email_name(self, email):
    return self.email.split("@")[0]

def get_email_domain(self, email):
    return self.email.split("@")[1]
Sudheer Singh
  • 555
  • 5
  • 10
-1

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("johns123.ss@google.com"))   #Output is - johns123
print(returnUserName('xjhgjg876896@domain.com'))  #Output is - xjhgjg876896