18

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
  • 505
  • 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

11 Answers11

60

You should just use the split method of strings:

s.split("@")[0]
abarnert
  • 334,953
  • 41
  • 559
  • 636
David Robinson
  • 74,512
  • 15
  • 159
  • 179
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,807
  • 3
  • 24
  • 32
Tuxdude
  • 44,385
  • 13
  • 103
  • 107
3

You shouldn't use a regex or split.

local, at, domain = 'john.smith@example.org'.rpartition('@')
jhrr
  • 1,480
  • 13
  • 20
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,154
  • 52
  • 64
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

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.

shantanoo
  • 3,587
  • 1
  • 23
  • 36
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,807
  • 3
  • 24
  • 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

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
Sourav3365
  • 11
  • 3