Python Practice › RegEx Programs › Program 1
Validate email addresses
Write a Python program to check which strings are valid email addresses.
Output
asha@sqatools.in True ravi.k+qa@mail.co.uk True bad@ False no space@x.com False
Approach
re.fullmatchwith a practical email pattern.
Solution
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.