Validate email addresses

EasyRegEx Programs · 1 of 20

Write a Python program to check which strings are valid email addresses.

Advertisement
Output
asha@sqatools.in       True
ravi.k+qa@mail.co.uk   True
bad@                   False
no space@x.com         False

Approach

  1. re.fullmatch with a practical email pattern.

Solution

Python
import re pattern = re.compile(r"[\w.+-]+@[\w-]+(\.[\w-]+)+")for e in ["asha@sqatools.in", "ravi.k+qa@mail.co.uk", "bad@", "no space@x.com"]:    print(e.ljust(22), bool(pattern.fullmatch(e)))
Output
asha@sqatools.in       True
ravi.k+qa@mail.co.uk   True
bad@                   False
no space@x.com         False

Line by line

Line 3

Name part, @, a domain and at least one dot-separated part.

Note

No regex covers every legal email address; for sign-ups, send a confirmation email instead of relying on the pattern alone.

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Leave a Comment