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:
- import re
- def get_email_id(inputString):
- emailAddresses= re.findall('(?: |^)[a-zA-Z][0-9a-zA-Z_]*@[0-9a-zA-Z-]+\.[0-9a-zA-Z]+(?:\.[0-9a-zA-Z]+)*',inputString)
- emailAddresses = [emailID.lstrip() for emailID in emailAddresses]
- return emailAddresses
- if __name__=="__main__":
- 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
- ['abc@def.com', 'xyz@def.co.uk']
The user may pass any string as the argument to the function.