Thursday, August 31, 2017

Extracting Email id Using Python Regular Expression



Regular expression is handy for extracting important part from a document or a string.  Suppose you want to extract email id from a string then it can be easily done by '\S+@\S+' this expression. Where \S represents non-whitespace character, then followed by an @ sign and again by \S;as actually it is in a generic email id. So , let's get the code and extract email id - 


import re

str = 'I have tried to email you at medhrk@gmail.com but it did not work so I send the mail to you by using your rediff account idhrk@rediffmail.com  at  @2PM'

lst = re.findall('\S+@\S+', str)

print lst  


The output will be  - ['medhrk@gmail.com',ídhrk@rediffmail.com],  

No comments:

Post a Comment

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...