Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regex get first part of an email address

Tags:

python

regex

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.

like image 874
JasonB Avatar asked Mar 04 '13 20:03

JasonB


People also ask

How do I get the contents of an email in Python?

The easiest tool I've found for reading emails in Python is imap_tools. It has an elegant interface to communicate with your email provider using IMAP (which almost every email provider will have). First, you access the MailBox; for which you need to get the IMAP server and login credentials (username and password).

How do I fetch an email from a string in Python?

To extract emails form text, we can take of regular expression. In the below example we take help of the regular expression package to define the pattern of an email ID and then use the findall() function to retrieve those text which match this pattern.

WHAT IS RE sub in Python?

sub() function belongs to the Regular Expressions ( re ) module in Python. It returns a string where all matching occurrences of the specified pattern are replaced by the replace string. To use this function, we need to import the re module first. import re.


1 Answers

You should just use the split method of strings:

s.split("@")[0]
like image 81
David Robinson Avatar answered Oct 14 '22 17:10

David Robinson