Table of Contents
- Introduction
- Prerequisites
- Setup
- Overview
- Exercise 1: Validating Email Addresses
- Exercise 2: Extracting Phone Numbers from Text
- Exercise 3: Parsing CSV Files
- Conclusion
Introduction
In this tutorial, we will practice using regular expressions in Python through a series of exercises. Regular expressions (regex) are powerful tools for pattern matching and data manipulation. By the end of this tutorial, you will have a better understanding of how to leverage regular expressions in your Python code to solve common problems.
Prerequisites
To follow along with this tutorial, you should have a basic understanding of Python programming concepts, including string manipulation and basic regular expression syntax. Familiarity with regular expressions in other programming languages is a plus.
Setup
Before we begin, ensure that Python is installed on your machine. You can check the Python version by opening a terminal or command prompt and running the following command:
python
python --version
If Python is not installed, you can download and install it from the official Python website (https://www.python.org).
Additionally, we will be using the re
module, which is a built-in Python module for working with regular expressions. This module is included in the Python standard library, so no additional installation is required.
Overview
In this tutorial, we will cover three practical exercises that demonstrate the usage of regular expressions in Python. The exercises are as follows:
- Validating Email Addresses: We will write a Python program to validate the format of email addresses using regular expressions.
- Extracting Phone Numbers from Text: We will extract phone numbers from a given text using regular expressions.
- Parsing CSV Files: We will use regular expressions to parse and extract data from a CSV (Comma-Separated Values) file.
Let’s get started with our first exercise.
Exercise 1: Validating Email Addresses
Email addresses follow a specific format, and we can use regular expressions to validate whether an email address is correctly formatted. Below is an example Python code that validates email addresses using regular expressions: ```python import re
def validate_email(email):
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
if re.match(pattern, email):
return True
else:
return False
email = input("Enter an email address: ")
if validate_email(email):
print("Valid email address")
else:
print("Invalid email address")
``` Explanation: - We import the `re` module, which provides functions for working with regular expressions. - The `validate_email` function takes an email address as input. - The `pattern` variable defines the regular expression pattern for validating email addresses. This pattern ensures that the email address starts with one or more alphanumeric characters or dots or hyphens (`[\w\.-]+`), followed by the `@` symbol, followed by one or more alphanumeric characters or dots (`[\w\.-]+`), followed by a dot and one or more alphanumeric characters (`\.\w+`). - The `re.match` function is used to match the pattern against the input email. If there is a match, the email is considered valid. - Finally, the script prompts the user to enter an email address, validates it using the `validate_email` function, and prints the result.
Now, let’s move on to the next exercise.
Exercise 2: Extracting Phone Numbers from Text
In this exercise, we will use regular expressions to extract phone numbers from a given text. Here’s a Python code example that demonstrates this: ```python import re
text = "My phone number is (123) 456-7890. Please call me!"
pattern = r'\(\d{3}\) \d{3}-\d{4}'
phone_numbers = re.findall(pattern, text)
print("Phone numbers found:")
for number in phone_numbers:
print(number)
``` Explanation: - We import the `re` module, as before. - The `text` variable stores the input text from which we want to extract phone numbers. - The `pattern` variable defines the regular expression pattern for matching phone numbers. The pattern `r'\(\d{3}\) \d{3}-\d{4}'` matches phone numbers in the format `(xxx) xxx-xxxx`, where `x` represents a digit. - The `re.findall` function is used to find all matches of the pattern in the input text. It returns a list of all matching phone numbers. - Finally, we iterate over the list of phone numbers and print each number.
Let’s move on to our final exercise.
Exercise 3: Parsing CSV Files
CSV files are commonly used for storing tabular data. In this exercise, we will use regular expressions to parse and extract data from a CSV file. Here’s an example Python code that accomplishes this: ```python import re
def parse_csv(file_path):
with open(file_path, 'r') as file:
data = file.read()
pattern = r'(\w+),(\w+),(\w+)'
matches = re.findall(pattern, data)
table = []
for match in matches:
table.append(list(match))
return table
file_path = 'data.csv'
table = parse_csv(file_path)
print("Parsed data:")
for row in table:
print(row)
``` Explanation: - We import the `re` module, as before. - The `parse_csv` function takes a file path as input and returns a two-dimensional list representing the parsed data from the CSV file. - Inside the function, we open the CSV file using the `open` function and read its contents into the `data` variable. - The `pattern` variable defines the regular expression pattern for matching a single row in the CSV file. The pattern `r'(\w+),(\w+),(\w+)'` matches three alphanumeric fields separated by commas. Modify the pattern according to your CSV file format. - The `re.findall` function is used to find all matches of the pattern in the `data` variable. It returns a list of tuples, where each tuple represents a row in the CSV file. - We then convert each tuple to a list and append it to the `table` list. - Finally, we iterate over the `table` and print each row.
Conclusion
In this tutorial, we have covered three practical exercises that demonstrate the usage of regular expressions in Python. We learned how to validate email addresses, extract phone numbers from text, and parse data from a CSV file using regular expressions. Regular expressions are powerful tools that can greatly simplify string manipulation and pattern matching tasks. With practice and experimentation, you can leverage regular expressions to solve a wide range of problems in your Python projects.
Remember to import the re
module whenever you need to work with regular expressions in Python, and familiarize yourself with the various regex syntax elements and functions available in the module. Happy coding!