Regular Expressions

Introduction to Regular Expressions

This topic provides an introduction to regular expressions (regex) in Python. Regular expressions are powerful tools for pattern matching and text manipulation. You'll learn about the basic syntax of regular expressions, commonly used metacharacters, and how to use the re module in Python to work with regular expressions. Understanding regular expressions is essential for tasks such as text validation, extraction, and substitution.

  1. YouTube Video Link: Introduction to Regular Expressions in Python - Please note that the provided link is a placeholder. You can search for relevant videos on popular platforms like YouTube or other online learning platforms.

Examples

Example 1: Matching a Pattern

import re

text = "Hello, my name is John. Nice to meet you, John!"

# Search for the word "John" in the text
result = re.search(r"John", text)
if result:
    print("Match found!")
else:
    print("No match.")

Example 2: Using Metacharacters

import re

text = "The cat and the hat sat on the mat."

# Find all words ending with "at"
matches = re.findall(r"\w+at", text)
print(matches)

Example 3: Extracting Email Addresses

import re

text = "Contact us at [email protected] or [email protected]"

# Extract all email addresses from the text
emails = re.findall(r"\S+@\S+", text)
print(emails)

Example 4: Validating Phone Numbers

import re

def validate_phone_number(phone_number):
    pattern = r"\d{3}-\d{3}-\d{4}"  # Pattern: XXX-XXX-XXXX
    if re.match(pattern, phone_number):
        return True
    return False

# Validate a phone number
number = "123-456-7890"
if validate_phone_number(number):
    print("Phone number is valid.")
else:
    print("Phone number is invalid.")

Example 5: Substituting Text

import re

text = "Hello, my name is John. Nice to meet you, John!"

# Replace all occurrences of "John" with "Alice"
new_text = re.sub(r"John", "Alice", text)
print(new_text)

Exercises

Exercise 1: Write a program that prompts the user to enter a string and checks if it is a valid email address using regular expressions.

Exercise 2: Create a function called extract_urls that takes a string as input and returns a list of all URLs found in the string using regular expressions.

Exercise 3: Write a program that prompts the user to enter a text and a search pattern. The program should find all occurrences of the pattern in the text using regular expressions and display the matches.

Exercise 4: Create a function called count_words that takes a string as input and returns the count of words in the string using regular expressions.

Exercise 5: Write a program that prompts the user to enter a list of phone numbers. The program should validate each phone number using regular expressions and display whether it is valid or invalid.

Pattern Matching and Searching

This topic focuses on pattern matching and searching techniques in Python. You'll learn how to use regular expressions and built-in string methods to match patterns and search for specific substrings within text data. Understanding these techniques is crucial for tasks such as data extraction, text processing, and information retrieval.

  1. YouTube Video Link: Pattern Matching and Searching in Python - Please note that the provided link is a placeholder. You can search for relevant videos on popular platforms like YouTube or other online learning platforms.

Examples

Example 1: Using Regular Expressions for Pattern Matching

import re

text = "The quick brown fox jumps over the lazy dog."

# Search for words starting with 'q' and ending with 'x'
matches = re.findall(r"\bq\w*x\b", text)
print(matches)

Example 2: Using str.find() Method for Substring Search

text = "The quick brown fox jumps over the lazy dog."

# Find the index of the word 'fox'
index = text.find('fox')
print(index)

Example 3: Using str.startswith() and str.endswith() Methods

text = "The quick brown fox jumps over the lazy dog."

# Check if the text starts with 'The'
starts_with_the = text.startswith('The')
print(starts_with_the)

# Check if the text ends with 'dog.'
ends_with_dog = text.endswith('dog.')
print(ends_with_dog)

Example 4: Using str.count() Method for Counting Occurrences

text = "The quick brown fox jumps over the lazy dog."

# Count the occurrences of the letter 'o'
count = text.count('o')
print(count)

Example 5: Using str.index() Method for Finding Substring Index

text = "The quick brown fox jumps over the lazy dog."

# Find the index of the word 'jumps'
index = text.index('jumps')
print(index)

Exercises

Exercise 1: Write a program that prompts the user to enter a sentence and a search word. The program should find the first occurrence of the word in the sentence and display its index.

Exercise 2: Create a function called find_emails that takes a string as input and returns a list of all email addresses found in the string.

Exercise 3: Write a program that prompts the user to enter a paragraph and a search term. The program should count and display the number of occurrences of the search term in the paragraph.

Exercise 4: Create a function called extract_phone_numbers that takes a string as input and returns a list of all phone numbers found in the string.

Exercise 5: Write a program that prompts the user to enter a sentence and a substring. The program should check if the substring exists within the sentence and display a message indicating whether it is found or not.

Replacing and Manipulating Strings using Patterns

This topic focuses on techniques for replacing and manipulating strings using patterns in Python. You'll learn how to use regular expressions and string methods to identify specific patterns in text data and perform various operations like replacing substrings, removing unwanted characters, and transforming the text based on predefined rules. Understanding these techniques is essential for tasks such as data cleaning, text preprocessing, and string manipulation.

  1. YouTube Video Link: Replacing and Manipulating Strings using Patterns in Python - Please note that the provided link is a placeholder. You can search for relevant videos on popular platforms like YouTube or other online learning platforms.

Examples

Example 1: Using str.replace() Method for Simple Substring Replacement

text = "Hello, World!"

# Replace 'Hello' with 'Hi'
new_text = text.replace('Hello', 'Hi')
print(new_text)

Example 2: Using Regular Expressions for Pattern-based Replacement

import re

text = "Hello, my name is John. Nice to meet you, John!"

# Replace all occurrences of 'John' with 'Alice'
new_text = re.sub(r"John", "Alice", text)
print(new_text)

Example 3: Removing Unwanted Characters

import re

text = "The price is $19.99."

# Remove all non-digit characters
clean_text = re.sub(r"\D", "", text)
print(clean_text)

Example 4: Transforming Text using Regular Expressions

import re

text = "Hello, my name is John Doe."

# Transform the text to uppercase
uppercase_text = re.sub(r"\b\w+\b", lambda match: match.group().upper(), text)
print(uppercase_text)

Example 5: Replacing Words based on a Dictionary

text = "I love eating apples and oranges."

# Replace words based on a dictionary
word_mapping = {"apples": "bananas", "oranges": "pears"}
new_text = " ".join(word_mapping.get(word, word) for word in text.split())
print(new_text)

Exercises

Exercise 1: Write a program that prompts the user to enter a sentence and a word to be replaced. The program should replace all occurrences of the word with '***' and display the modified sentence.

Exercise 2: Create a function called remove_punctuation that takes a string as input and returns the string with all punctuation characters removed.

Exercise 3: Write a program that prompts the user to enter a paragraph and a search term. The program should replace all occurrences of the search term with a highlighted version, surrounded by asterisks (*).

Exercise 4: Create a function called capitalize_names that takes a string as input and returns the string with all names capitalized. Assume that names are represented by a sequence of alphabetic characters and are separated by spaces.

Exercise 5: Write a program that prompts the user to enter a sentence and a word. The program should replace all occurrences of the word in the sentence with a lowercase version of the word, surrounded by parentheses.

Last updated

Was this helpful?