python

exercises

exercises.py🐍
"""
13 - Regex: Exercises
Practice regular expressions.
"""

print("=" * 60)
print("REGEX EXERCISES")
print("=" * 60)

import re

# =============================================================================
# EXERCISE 1: Find All Numbers
# Extract all numbers (including decimals) from a string.
# =============================================================================
print("\n--- Exercise 1: Find Numbers ---")

# Your code here:


# Test:
# text = "The price is $19.99 and quantity is 5"
# print(find_numbers(text))  # ['19.99', '5']


# =============================================================================
# EXERCISE 2: Validate Email
# Create a function to validate email addresses.
# =============================================================================
print("\n--- Exercise 2: Email Validation ---")

# Your code here:


# Test:
# print(is_valid_email("user@example.com"))  # True
# print(is_valid_email("invalid.email"))     # False


# =============================================================================
# EXERCISE 3: Extract URLs
# Extract all URLs from a text.
# =============================================================================
print("\n--- Exercise 3: Extract URLs ---")

# Your code here:


# Test:
# text = "Visit https://google.com or http://example.org/page"
# print(extract_urls(text))


# =============================================================================
# EXERCISE 4: Phone Number Formatter
# Convert various phone formats to (XXX) XXX-XXXX.
# =============================================================================
print("\n--- Exercise 4: Phone Formatter ---")

# Your code here:


# Test:
# print(format_phone("1234567890"))     # (123) 456-7890
# print(format_phone("123-456-7890"))   # (123) 456-7890


# =============================================================================
# EXERCISE 5: Password Strength
# Check password strength (length, uppercase, lowercase, digit, special).
# =============================================================================
print("\n--- Exercise 5: Password Strength ---")

# Your code here:


# Test:
# print(check_password("weak"))
# print(check_password("Strong1!Pass"))


# =============================================================================
# EXERCISE 6: HTML Tag Extractor
# Extract all HTML tags from content.
# =============================================================================
print("\n--- Exercise 6: Extract HTML Tags ---")

# Your code here:


# Test:
# html = "<div class='main'><p>Hello</p></div>"
# print(extract_tags(html))  # ['div', 'p']


# =============================================================================
# EXERCISE 7: Date Parser
# Parse dates in various formats (DD/MM/YYYY, YYYY-MM-DD, etc.)
# =============================================================================
print("\n--- Exercise 7: Date Parser ---")

# Your code here:


# Test:
# print(parse_date("25/12/2024"))
# print(parse_date("2024-12-25"))


# =============================================================================
# EXERCISE 8: Censor Words
# Replace specific words with asterisks.
# =============================================================================
print("\n--- Exercise 8: Censor Words ---")

# Your code here:


# Test:
# text = "This is bad and very bad behavior"
# print(censor(text, ["bad", "behavior"]))


# =============================================================================
# EXERCISE 9: Extract Quoted Strings
# Extract all strings enclosed in quotes.
# =============================================================================
print("\n--- Exercise 9: Extract Quoted Strings ---")

# Your code here:


# Test:
# text = 'He said "Hello" and she replied "Hi there"'
# print(extract_quoted(text))


# =============================================================================
# EXERCISE 10: Log Line Parser
# Parse log lines into components (timestamp, level, message).
# =============================================================================
print("\n--- Exercise 10: Log Parser ---")

# Your code here:


# Test:
# log = "2024-01-15 10:30:45 ERROR Database connection failed"
# print(parse_log(log))


# =============================================================================
# SOLUTIONS
# =============================================================================
print("\n\n" + "=" * 60)
print("SOLUTIONS")
print("=" * 60)

# SOLUTION 1
print("\n--- Solution 1: Find Numbers ---")

def find_numbers(text):
    return re.findall(r"\d+\.?\d*", text)

text = "The price is $19.99 and quantity is 5"
print(f"Numbers: {find_numbers(text)}")

# SOLUTION 2
print("\n--- Solution 2: Email Validation ---")

def is_valid_email(email):
    pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
    return bool(re.match(pattern, email))

print(f"user@example.com: {is_valid_email('user@example.com')}")
print(f"invalid.email: {is_valid_email('invalid.email')}")
print(f"test@: {is_valid_email('test@')}")

# SOLUTION 3
print("\n--- Solution 3: Extract URLs ---")

def extract_urls(text):
    pattern = r"https?://[^\s]+"
    return re.findall(pattern, text)

text = "Visit https://google.com or http://example.org/page"
print(f"URLs: {extract_urls(text)}")

# SOLUTION 4
print("\n--- Solution 4: Phone Formatter ---")

def format_phone(phone):
    # Remove all non-digits
    digits = re.sub(r"\D", "", phone)
    if len(digits) == 10:
        return f"({digits[:3]}) {digits[3:6]}-{digits[6:]}"
    return None

print(f"1234567890: {format_phone('1234567890')}")
print(f"123-456-7890: {format_phone('123-456-7890')}")
print(f"(123) 456-7890: {format_phone('(123) 456-7890')}")

# SOLUTION 5
print("\n--- Solution 5: Password Strength ---")

def check_password(password):
    checks = {
        "min_length": len(password) >= 8,
        "uppercase": bool(re.search(r"[A-Z]", password)),
        "lowercase": bool(re.search(r"[a-z]", password)),
        "digit": bool(re.search(r"\d", password)),
        "special": bool(re.search(r"[!@#$%^&*(),.?\":{}|<>]", password)),
    }
    strength = sum(checks.values())
    
    if strength == 5:
        level = "Strong"
    elif strength >= 3:
        level = "Medium"
    else:
        level = "Weak"
    
    return {"checks": checks, "strength": level}

print(f"weak: {check_password('weak')['strength']}")
print(f"Strong1!Pass: {check_password('Strong1!Pass')['strength']}")

# SOLUTION 6
print("\n--- Solution 6: Extract HTML Tags ---")

def extract_tags(html):
    pattern = r"<(\w+)"
    return list(set(re.findall(pattern, html)))

html = "<div class='main'><p>Hello</p><span>World</span></div>"
print(f"Tags: {extract_tags(html)}")

# SOLUTION 7
print("\n--- Solution 7: Date Parser ---")

def parse_date(date_string):
    patterns = [
        (r"(\d{1,2})/(\d{1,2})/(\d{4})", "DMY"),
        (r"(\d{4})-(\d{1,2})-(\d{1,2})", "YMD"),
        (r"(\d{1,2})-(\d{1,2})-(\d{4})", "DMY"),
    ]
    
    for pattern, format_type in patterns:
        match = re.match(pattern, date_string)
        if match:
            if format_type == "DMY":
                day, month, year = match.groups()
            else:  # YMD
                year, month, day = match.groups()
            return {"day": int(day), "month": int(month), "year": int(year)}
    
    return None

print(f"25/12/2024: {parse_date('25/12/2024')}")
print(f"2024-12-25: {parse_date('2024-12-25')}")

# SOLUTION 8
print("\n--- Solution 8: Censor Words ---")

def censor(text, words):
    pattern = r"\b(" + "|".join(words) + r")\b"
    return re.sub(pattern, lambda m: "*" * len(m.group()), text, flags=re.IGNORECASE)

text = "This is bad and very bad behavior"
print(f"Original: {text}")
print(f"Censored: {censor(text, ['bad', 'behavior'])}")

# SOLUTION 9
print("\n--- Solution 9: Extract Quoted ---")

def extract_quoted(text):
    return re.findall(r'"([^"]*)"', text)

text = 'He said "Hello" and she replied "Hi there"'
print(f"Quoted: {extract_quoted(text)}")

# SOLUTION 10
print("\n--- Solution 10: Log Parser ---")

def parse_log(log_line):
    pattern = r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (\w+) (.+)"
    match = re.match(pattern, log_line)
    if match:
        return {
            "timestamp": match.group(1),
            "level": match.group(2),
            "message": match.group(3),
        }
    return None

log = "2024-01-15 10:30:45 ERROR Database connection failed"
print(f"Parsed: {parse_log(log)}")

print("\n" + "=" * 60)
print("END OF EXERCISES")
print("=" * 60)
Exercises - Python Tutorial | DeepML