Get startedGet started for free

Give me your email

A colleague has asked for your help! When a user signs up on the company website, they must provide a valid email address.
The company puts some rules in place to verify that the given email address is valid:

  • The first part can contain:
    • Upper A-Z or lowercase letters a-z
    • Numbers
    • Characters: !, #, %, &, *, $, .
  • Must have @
  • Domain:
    • Can contain any word characters
    • But only .com ending is allowed

The project consists of writing a script that checks if the email address follow the correct pattern. Your colleague gave you a list of email addresses as examples to test.

The list emails as well as the re module are loaded in your session. You can use print(emails) to view the emails in the IPython Shell.

This exercise is part of the course

Regular Expressions in Python

View Course

Exercise instructions

  • Write a regular expression to match valid email addresses as described.
  • Match the regex to the elements contained in emails.
  • To print out the message indicating if it is a valid email or not, complete .format() statement.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Write a regex to match a valid email address
regex = ____"[____]____@____\.com"

for example in emails:
  	# Match the regex to the string
    if re.____(____, ____):
        # Complete the format method to print out the result
      	print("The email {____} is a valid email".____(email_example=____))
    else:
      	print("The email {____} is invalid".____(email_example=____))   
Edit and Run Code