Skip to the content

Project: pull out the emails

Python · Unit 18: Regular expressions

You're going to work with emails. The rule for this project: an email is \w+, an at sign, \w+ and .com.

The dot is written \. to ask for a real dot and not for any character. A real validator is longer; this one is enough to learn with.

import re

p = r"\w+@\w+\.com"
t = "amy@mail.com"
print(bool(re.fullmatch(p, t)))

Prints

True

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Complete the code

    is_email should accept only emails ending in .com. Complete the pattern.

  2. 2. Write the code

    Write extract(t): it gives back the list of emails in the text, in the order they show up. If there are none, the list comes back empty. Use the same email rule as the step before.

  3. 3. Write the code

    Write censor(t): it gives back the same text, but with every email swapped for ***. If there are no emails, the text comes out unchanged. The pattern is already written.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →