python regex get first part of an email address

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.

Tags:

Python

Regex