-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathextract_emails.py
More file actions
35 lines (31 loc) · 1.05 KB
/
extract_emails.py
File metadata and controls
35 lines (31 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
################################################################################
#
# Program: Extract Emails From Text
#
# Description: Extract emails from text using a regular expresion in Python.
#
# YouTube Lesson: https://www.youtube.com/watch?v=L7OXfdoH80I
#
# Author: Kevin Browne @ https://portfoliocourses.com
#
################################################################################
import re
# Returns all email addresses found in text.
#
# Uses a pattern to extract all the email addresses from the text using the
# regular expression module findall function. Note that the pattern will not
# match 100% of email addresses due to edge cases, but should match for 99%+ of
# common email addresses.
#
def extract_emails(text):
pattern = r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"
return re.findall(pattern, text)
# Example text
text = """
bla bla kevin@gmail.com more text joe.black@canada.ca more
text nageeb_ali@company.co.uk
"""
# Test the function
emails = extract_emails(text)
for email in emails:
print(email)