LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Regular Expressions in Python

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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=____))   
Code bearbeiten und ausführen