You are on page 1of 5

Practical - 1

Aim:
Write a Python program that uses regular expressions to validate email addresses.

Theory:
This practical will demonstrate how to validate email addresses using regular expressions in
Python. Regular expressions provide a powerful and flexible way to search, match, and validate
text patterns, making them ideal for tasks like email address validation.

Concepts Covered:
1. Regular Expressions (Regex):
Regular expressions, often abbreviated as regex or regexp, are powerful tools for
working with patterns in text data. A regular expression is a sequence of characters that
defines a search pattern. It allows you to match, search, and manipulate text based on a
specified pattern.
Key Concepts:
 Characters and Metacharacters: Regular expressions consist of normal characters
(e.g., letters and digits) and metacharacters (e.g., '*', '.', '^'), which have special
meanings in the context of regex.
 Quantifiers: Quantifiers determine the number of occurrences of a character or
group in a regex (e.g., '*', '+', '?', '{n}', '{m,n}').
 Anchors: Anchors are used to define the position in the string (e.g., '^' for the start
of a line, '$' for the end of a line).
 Character Classes: Character classes match any one of a set of characters (e.g., '[a-
z]' matches any lowercase letter).
 Groups and Capturing: Groups are used to define sub-patterns, and capturing allows
you to extract portions of the matched text.
 Escape Characters: The backslash () is used to escape metacharacters, allowing you
to match them as regular characters.

Regular expressions are widely used for tasks like data validation, searching and
replacing, text extraction, and parsing.

2. Python's re Module:
Python's re module provides functions and methods to work with regular expressions
in Python. Here are some key functions:
 re.match(pattern, string): Attempts to match the pattern at the beginning of the
string. Returns a match object if successful, None otherwise.
 re.search(pattern, string): Searches the string for a match using the pattern. Returns
a match object if a match is found, None otherwise.
 re.findall(pattern, string): Finds all occurrences of the pattern in the string and
returns them as a list of strings.
 re.finditer(pattern, string): Finds all occurrences of the pattern in the string and
returns an iterator of match objects.
 re.sub(pattern, replacement, string): Replaces occurrences of the pattern in the
string with the specified replacement.
These functions allow you to perform various operations like matching, searching,
finding all occurrences, and replacing using regular expressions.
3. Email Address Validation using Regex:
Validating email addresses using regular expressions involves defining a pattern that
matches the common structure of email addresses. An email address typically follows
the pattern username@domain.com. Here's a breakdown of a basic regex pattern for
email validation:
 ^ asserts the start of the string.
 [a-zA-Z0-9._%+-]+ matches the username part, allowing letters, digits, dots,
underscores, percent signs, plus signs, and hyphens.
 @ matches the "@" symbol.
 [a-zA-Z0-9.-]+ matches the domain part, allowing letters, digits, dots, and
hyphens.
 \. matches a dot (.)
 [a-zA-Z]{2,} matches the top-level domain (e.g., "com") with at least two
characters.
 $ asserts the end of the string.

Using this pattern, we can validate email addresses to ensure they follow a common
email format.

Applications:
Regular expressions for email validation, as demonstrated in the provided code, are widely
used in various applications and systems for ensuring that the entered email addresses adhere
to a standard format. Here are some real-life use cases where email validation using regular
expressions is crucial:
1. User Registration and Authentication:
When users sign up for a website or application, they are often required to provide an
email address. Regular expressions can be used to validate the email addresses during
the registration process to ensure they are in the correct format before storing them in a
database.
2. Contact Forms on Websites:
Websites often have contact forms that allow visitors to get in touch. These forms
usually ask for the user's email address. Email validation using regular expressions
helps ensure that the email addresses provided through these forms are valid.
3. E-commerce Websites:
E-commerce platforms ask users for their email addresses during the checkout process
or for subscribing to newsletters. Email validation helps ensure that the entered
addresses are properly formatted, reducing errors and improving communication.
4. Email Marketing and Newsletters:
In email marketing campaigns, businesses use regular expressions to validate and clean
email lists, ensuring that the email addresses are valid and correctly formatted before
sending out newsletters or marketing materials.
5. Account Recovery and Password Reset:
When users request account recovery or password reset links, they typically need to
provide their email addresses. Regular expression-based validation helps ensure that
the email address is valid before proceeding with the recovery/reset process.
6. APIs and Web Services:
APIs that require email addresses as input can use regular expressions to validate the
email addresses received as parameters, ensuring the correct format before processing
the requests.
7. Internal Systems and Databases:
In internal applications or systems within an organization, regular expressions can be
used to validate email addresses before storing them in databases or using them for
communication purposes.

Algorithm: Email Address Validation using Regular Expressions:


Input: User input for an email address to be validated.
Output: Display whether the entered email address is valid or not.
Steps:
1. Import the re Module: Import the re module to use regular expressions for pattern
matching.
2. Define a Function to Validate Email Addresses:
 Define a function is_valid_email that takes an email address as input and returns a
boolean indicating whether the email is valid or not. Define a regular expression pattern
that matches a common structure of an email address:
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-
Z]{2,}$'
 Use re.match() to check if the email matches the pattern and return True if it does,
indicating a valid email address.
3. Main Program:
 Run the following steps in a loop until the user decides to exit:
a. Prompt the user to enter an email address or 'exit' to quit.
b. If the user input is 'exit', break the loop and end the program.
c. If the user input is not 'exit':
o Call the is_valid_email function with the user-entered email address as
an argument.
o If the email is valid, display a message indicating it is a valid email
address.
o If the email is not valid, display a message indicating it is not a valid
email address.

PseudoCode:

Import the `re` module

Function is_valid_email(email):
Define a regular expression pattern for email validation
Use `re.match()` to match the email against the pattern
If the match is found, return True (indicating a valid email)
Else, return False

Main Program:
Loop forever:
Prompt the user to enter an email address or 'exit' to quit
If user input is 'exit', break the loop
If user input is not 'exit':
Call is_valid_email with the entered email as an argument
If the email is valid:
Display a message indicating it is a valid email address
Else:
Display a message indicating it is not a valid email address
Code Explanation:

import re
 This line imports the re module, which provides support for working with regular
expressions in Python.

def is_valid_email(email):
 This line defines a function named is_valid_email that takes an email as a parameter.

pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
 This line defines a regular expression pattern to validate an email address. The pattern
checks for a typical email format, including characters before and after the '@' symbol,
a domain, and a top-level domain (TLD). It ensures the TLD has at least 2 characters.

return re.match(pattern, email) is not None


 This line uses the re.match() function to match the email against the defined pattern. If
the email matches the pattern, re.match() will return a match object (not None),
indicating a valid email. Otherwise, it returns None, indicating an invalid email.

if __name__ == "__main__":
 This line checks if the script is being run directly (as opposed to being imported as a
module).

while True:
 This line starts an infinite loop, prompting the user for an email address repeatedly until
they choose to exit.

email = input("Enter an email address (or 'exit' to quit): ")


 This line prompts the user to input an email address or type 'exit' to quit.

if email.lower() == 'exit': break


 This line checks if the user input is 'exit'. If so, it breaks out of the loop, ending the
program.

if is_valid_email(email): print(f"{email} is a valid email address.") else: print(f"{email}


is not a valid email address.")
 This block of code calls the is_valid_email function to validate the user's input. It then
prints whether the entered email address is valid or not.
Code and Output:

You might also like