Python regex gets first part of email address

I am new to python and regex, and I was wondering how to extract the first part of the email address before the domain name. For example, if:

s='xjhgjg876896@domain.com'

I would like the result of a regular expression (given all the "sortings" of email identifiers, including numbers, etc.):

xjhgjg876896

I get an idea of ​​regex - since I know that I need to scan to "@" and then save the result, but I'm not sure how to implement this in python.

Thank you for your time.

+9
source share
8 answers

You should use the splitstring method :

s.split("@")[0]
+35
source

, split.

regex, :

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"

foo

.. SOMEONE@SOMETHING.TLD. NAME<SOMEONE@SOMETHING.TLD>, .

+4

EmailExtractor.py. . . "@" () .

0

, .

  • john@gmail.com, "john".

    "john"

  • john.joe@gmail.com, "john"

    "john"

:

name = recipient.split("@")[0]
name = name.split(".")[0]
print name

0
#!/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')

:

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

email_split.

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

https://pypi.org/project/email_split/. :)

0

split.

local, at, domain = 'john.smith@example.org'.rpartition('@')
0

:

 fromAddr = message.get('From').split('@')[1].rstrip('>')
        fromAddr = fromAddr.split(' ')[0]
-1

All Articles