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.
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).
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.
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.
You should just use the split
method of strings:
s.split("@")[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With