Learning machine learning? Try my machine learning flashcards or Machine Learning with Python Cookbook.
Match Email Addresses
Preliminaries
# Load regex package
import re
Create some text
# Create a variable containing a text string
text = 'My email is [email protected], thanks! No, I am at [email protected]'
Apply regex
# Find all email addresses
re.findall(r'[a-zA-Z0-9_.+-][email protected][a-zA-Z0-9-]+\.[a-zA-Z0-9]+', text)
# Explanation:
# This regex has three parts
# [a-zA-Z0-9_.+-]+ Matches a word (the username) of any length
# @[a-zA-Z0-9-]+ Matches a word (the domain name) of any length
# \.[a-zA-Z0-9-.]+ Matches a word (the TLD) of any length
['[email protected]', '[email protected]']