10.4 Regular Expressions (RegEx)

10.4 Regular Expressions (RegEx)

Introduction to Regular Expressions with the "re" module for powerful pattern-based string searching, matching, and manipulation.

Regular Expressions (or RegEx) are a specialized mini-language used to describe patterns in text. They allow you to perform complex searches, validation checks, and replacements on strings with incredible flexibility and power. Python's `re` module provides all the necessary functions to work with RegEx.

Main Functions of re:

  • `re.search(pattern, string)`: Scans the entire string and returns the first match object it finds.
  • `re.match(pattern, string)`: Attempts to match the pattern only at the beginning of the string.
  • `re.findall(pattern, string)`: Finds all non-overlapping matches of the pattern and returns them as a list of strings.
  • `re.sub(pattern, replacement, string)`: Replaces all occurrences of the pattern with the `replacement` value.
import re

text = "My email is test@example.com and my phone number is 210-1234567."
email_pattern = r"[\w\.-]+@[\w\.-]+\.\w+"

match = re.search(email_pattern, text)
if match:
    print(f"Found email: {match.group()}")

# Find all numbers
numbers = re.findall(r"\d+", "Year 2024, Month 07, Day 18")
print(f"Found numbers: {numbers}")

# Hide phone numbers
new_text = re.sub(r"\d{3}-\d{7}", "XXX-XXXXXXX", text)
print(f"Text with hidden number: {new_text}")

Practical Exercises

Explore More with AI

Use AI to generate new examples, delve deeper into theory, or get your questions answered.