RegExp Based Email Address Filter

Regular Expressions have been one of the most successful attempts at standardization in Computer Science. Here we present a simple demonstration of their capabilities.

This Python Module extracts all valid email ID’s from a given string and returns them as a list of strings.

The code with an example string is follows here:

  1. import re
  2.  
  3. def get_email_id(inputString):
  4.     emailAddresses= re.findall('(?: |^)[a-zA-Z][0-9a-zA-Z_]*@[0-9a-zA-Z-]+\.[0-9a-zA-Z]+(?:\.[0-9a-zA-Z]+)*',inputString)
  5.     emailAddresses = [emailID.lstrip() for emailID in emailAddresses]
  6.     return emailAddresses
  7.  
  8.  
  9. if __name__=="__main__":
  10.     print get_email_id('ABC: I told you, my email is <a href="mailto:abc@def.com">abc@def.com</a> XYZ:Yes, thanks. Mine is <a href="mailto:xyz@def.co.uk">xyz@def.co.uk</a> ' )

Upon execution, the function get_email_id() will return the following list

  1. ['abc@def.com', 'xyz@def.co.uk']

The user may pass any string as the argument to the function.