Email Pattern Matching in Python Using Regex

Last Updated : 6 Feb, 2026

Email pattern matching is a common text‑processing task used to validate, extract or filter email addresses from raw text data. In Python, this is efficiently handled using Regex module, which allow precise pattern definition and matching based on email syntax rules. It is useful for data cleaning, form validation and log analysis.

Detecting Email Addresses in Text

A regular expression is used to check whether the given string matches a valid email address format. The re.match() function helps us to match the pattern from the beginning of the string to confirm the presence of a correctly structured email.

  • [A-Za-z0-9]+ matches the username part consisting of letters and digits.
  • @ represents the required separator between the username and domain.
  • [\w]+ matches the domain name such as gmail, yahoo or gfg.
  • \. matches the dot character.
  • [\w]+ matches the domain extension like com, org or in.
Python
import re

text = "eshant@gfg.in"
pattern = r'[A-Za-z0-9]+@[\w]+\.[\w]+'
print(re.match(pattern, text))

Output:

<re.Match object; span=(0, 13), match='eshant@gfg.in'>

Since we received a match object, it confirms that the email exists in the text.

Extracting Emails Using findall()

To extract only email addresses from a given text the findall() function is used. It returns all substrings that match the specified email pattern in the text.

Python
text = "Contact us at eshant@gfg.in for details"
pattern = r'[A-Za-z0-9]+@[\w]+\.[\w]+'
print(re.findall(pattern, text))

Output:

['eshant@gfg.in']

Here findall() returns only the matched email because the pattern is accurate.

Email ID with a Specific Domain

To match email addresses belonging to a specific domain the domain name is explicitly defined in the regex pattern. This ensures that only emails ending with that domain are matched.

Python
mail1 = "eshant@gfg.org"
mail2 = "eshant@gmail.com"

pattern = r'[A-Za-z0-9]+@gfg\.org'
print(re.match(pattern, mail1))
print(re.match(pattern, mail2))

Output:

<re.Match object; span=(0, 14), match='eshant@gfg.org'>
None

The first email matches because it belongs to gfg.org, while the second one does not.

Email ID with Multiple Selected Domains

When multiple domains are permitted the OR operator (|) is used within parentheses to match any one of the specified domains.

Python
mail1 = "eshant@gfg.org"
mail2 = "eshant@gfg.net"
mail3 = "eshant@gfg.com"

pattern = r'[A-Za-z0-9]+@gfg\.(org|net)'
print(re.match(pattern, mail1))
print(re.match(pattern, mail2))
print(re.match(pattern, mail3))

Output:

<re.Match object; span=(0, 14), match='eshant@gfg.org'>
<re.Match object; span=(0, 14), match='eshant@gfg.net'>
None

Emails with .org and .net are matched successfully, while .com is rejected because it is not included in the pattern.

Matching vs Searching Patterns in Python Regex

Both re.match() and re.search() are used to find patterns in text but they behave differently. re.match() checks for a match only at the beginning of the string, while re.search() scans the entire string for the pattern.

Python
import re

text = "Please contact us at eshant@gfg.in for support"
pattern = r"[A-Za-z0-9]+@[\w]+\.[\w]+"

print(re.match(pattern, text))
print(re.search(pattern, text))

Output:

None
<re.Match object; span=(21, 34), match='eshant@gfg.in'>

re.search() detects the email anywhere in the text, while re.match() fails because the email is not at the beginning.

Extracting Multiple Email Addresses from Text

A document or paragraph can contain more than one email address. The findall() function is used to extract all substrings that match a given email pattern, making it suitable for collecting multiple email addresses from text data.

Here we scans the entire text and returns a list of all email addresses that match the regex pattern

Python
import re

text = "Contact eshant@gfg.in or support@gfg.org for help"
pattern = r"[A-Za-z0-9]+@[\w]+\.[\w]+"

emails = re.findall(pattern, text)
print(emails)

Output:

['eshant@gfg.in', 'support@gfg.org']

Comment