v Match Email Addresses - Regex

Match Email Addresses

Based on: StackOverflow

Preliminaries

# Load regex package
import re

Create some text

# Create a variable containing a text string
text =  'My email is chris@hotmail.com, thanks! No, I am at bob@data.ninja.'

Apply regex

# Find all email addresses
re.findall(r'[a-zA-Z0-9_.+-]+@[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
['chris@hotmail.com', 'bob@data.ninja']