python

examples

examples.py🐍
"""
13 - Regex: Examples
Run this file to see regular expressions in action!
"""

print("=" * 60)
print("REGULAR EXPRESSIONS - EXAMPLES")
print("=" * 60)

import re

# =============================================================================
# 1. BASIC MATCHING
# =============================================================================
print("\n--- 1. Basic Matching ---\n")

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

# search - find first match
match = re.search(r"fox", text)
if match:
    print(f"Found '{match.group()}' at position {match.start()}")

# match - match at start only
match = re.match(r"The", text)
print(f"Starts with 'The': {match is not None}")

match = re.match(r"fox", text)
print(f"Starts with 'fox': {match is not None}")

# =============================================================================
# 2. FINDALL AND FINDITER
# =============================================================================
print("\n--- 2. findall and finditer ---\n")

text = "Contact us at info@example.com or support@example.org"

# findall - returns list of matches
emails = re.findall(r"\S+@\S+", text)
print(f"Emails found: {emails}")

# finditer - returns iterator with match objects
print("Email positions:")
for match in re.finditer(r"\S+@\S+", text):
    print(f"  {match.group()} at {match.start()}-{match.end()}")

# =============================================================================
# 3. SUB AND SPLIT
# =============================================================================
print("\n--- 3. sub and split ---\n")

text = "Hello   World    Python"

# sub - replace pattern
result = re.sub(r"\s+", " ", text)  # Multiple spaces to single
print(f"Cleaned: '{result}'")

# Split by pattern
text = "apple, banana; cherry orange"
parts = re.split(r"[,;\s]+", text)
print(f"Split: {parts}")

# =============================================================================
# 4. METACHARACTERS
# =============================================================================
print("\n--- 4. Metacharacters ---\n")

# . matches any character
text = "cat cot cut"
print(f"c.t matches: {re.findall(r'c.t', text)}")

# ^ and $ anchors
text = "Hello World"
print(f"^Hello matches: {bool(re.search(r'^Hello', text))}")
print(f"World$ matches: {bool(re.search(r'World$', text))}")

# | alternation
text = "I have a cat and a dog"
print(f"cat|dog matches: {re.findall(r'cat|dog', text)}")

# =============================================================================
# 5. CHARACTER CLASSES
# =============================================================================
print("\n--- 5. Character Classes ---\n")

text = "Hello123World456"

# [0-9] - digits
print(f"Digits: {re.findall(r'[0-9]+', text)}")

# [a-z] - lowercase
print(f"Lowercase: {re.findall(r'[a-z]+', text)}")

# [A-Za-z] - all letters
print(f"Letters: {re.findall(r'[A-Za-z]+', text)}")

# [^0-9] - not digits
print(f"Non-digits: {re.findall(r'[^0-9]+', text)}")

# =============================================================================
# 6. SPECIAL SEQUENCES
# =============================================================================
print("\n--- 6. Special Sequences ---\n")

text = "Phone: 123-456-7890, Email: test@example.com"

# \d - digits
print(f"\\d+: {re.findall(r'\\d+', text)}")

# \w - word characters
print(f"\\w+: {re.findall(r'\\w+', text)}")

# \s - whitespace
print(f"Split by \\s+: {re.split(r'\\s+', text)}")

# \b - word boundary
text = "cat category scattered"
print(f"\\bcat\\b: {re.findall(r'\\bcat\\b', text)}")

# =============================================================================
# 7. QUANTIFIERS
# =============================================================================
print("\n--- 7. Quantifiers ---\n")

text = "a ab abb abbb abbbb"

print(f"ab*: {re.findall(r'ab*', text)}")    # 0 or more b
print(f"ab+: {re.findall(r'ab+', text)}")    # 1 or more b
print(f"ab?: {re.findall(r'ab?', text)}")    # 0 or 1 b
print(f"ab{{2}}: {re.findall(r'ab{2}', text)}")    # exactly 2 b
print(f"ab{{2,3}}: {re.findall(r'ab{2,3}', text)}")  # 2-3 b

# =============================================================================
# 8. GREEDY VS NON-GREEDY
# =============================================================================
print("\n--- 8. Greedy vs Non-Greedy ---\n")

text = "<div>Hello</div><span>World</span>"

# Greedy (default)
print(f"Greedy <.*>: {re.findall(r'<.*>', text)}")

# Non-greedy
print(f"Non-greedy <.*?>: {re.findall(r'<.*?>', text)}")

# =============================================================================
# 9. GROUPS
# =============================================================================
print("\n--- 9. Groups ---\n")

text = "John Smith, age 30; Jane Doe, age 25"

# Capturing groups
pattern = r"(\w+) (\w+), age (\d+)"
for match in re.finditer(pattern, text):
    first, last, age = match.groups()
    print(f"Name: {first} {last}, Age: {age}")

# Named groups
pattern = r"(?P<first>\w+) (?P<last>\w+), age (?P<age>\d+)"
for match in re.finditer(pattern, text):
    print(f"Named: {match.groupdict()}")

# =============================================================================
# 10. BACKREFERENCES
# =============================================================================
print("\n--- 10. Backreferences ---\n")

# Find repeated words
text = "the the cat sat sat down"
pattern = r"\b(\w+)\s+\1\b"
duplicates = re.findall(pattern, text)
print(f"Repeated words: {duplicates}")

# Remove duplicates
fixed = re.sub(pattern, r"\1", text)
print(f"Fixed: {fixed}")

# =============================================================================
# 11. FLAGS
# =============================================================================
print("\n--- 11. Flags ---\n")

# IGNORECASE
text = "Hello HELLO hello"
print(f"Ignore case: {re.findall(r'hello', text, re.IGNORECASE)}")

# MULTILINE
text = "Line 1\nLine 2\nLine 3"
print(f"Multiline ^Line: {re.findall(r'^Line', text, re.MULTILINE)}")

# DOTALL
text = "Hello\nWorld"
print(f"Dotall Hello.World: {re.findall(r'Hello.World', text, re.DOTALL)}")

# =============================================================================
# 12. COMMON PATTERNS
# =============================================================================
print("\n--- 12. Common Patterns ---\n")

# Email
email_pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
text = "Contact: user@example.com, admin@test.org"
print(f"Emails: {re.findall(email_pattern, text)}")

# Phone
phone_pattern = r"\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}"
text = "Call: 123-456-7890 or (555) 123-4567"
print(f"Phones: {re.findall(phone_pattern, text)}")

# URL
url_pattern = r"https?://[\w.-]+(?:/[\w.-]*)?"
text = "Visit https://www.example.com/page or http://test.org"
print(f"URLs: {re.findall(url_pattern, text)}")

# =============================================================================
# 13. COMPILED PATTERNS
# =============================================================================
print("\n--- 13. Compiled Patterns ---\n")

# Compile for reuse
email_regex = re.compile(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")

emails = ["user@example.com", "invalid", "test@test.org", "@bad.com"]
print("Email validation:")
for email in emails:
    valid = "✓" if email_regex.match(email) else "✗"
    print(f"  {email}: {valid}")

# =============================================================================
# 14. PRACTICAL EXAMPLES
# =============================================================================
print("\n--- 14. Practical Examples ---\n")

# Extract dates
text = "Events: 2024-01-15, 2024-02-28, 2024-12-25"
date_pattern = r"(\d{4})-(\d{2})-(\d{2})"
print("Dates extracted:")
for match in re.finditer(date_pattern, text):
    year, month, day = match.groups()
    print(f"  Year: {year}, Month: {month}, Day: {day}")

# Clean HTML tags
html = "<p>Hello</p><strong>World</strong>"
clean = re.sub(r"<[^>]+>", "", html)
print(f"\nHTML stripped: {clean}")

# Extract hashtags
text = "Learning #Python and #RegEx is #fun!"
hashtags = re.findall(r"#(\w+)", text)
print(f"Hashtags: {hashtags}")

# Password validation
def validate_password(password):
    checks = {
        "length >= 8": len(password) >= 8,
        "has uppercase": bool(re.search(r"[A-Z]", password)),
        "has lowercase": bool(re.search(r"[a-z]", password)),
        "has digit": bool(re.search(r"\d", password)),
        "has special": bool(re.search(r"[!@#$%^&*]", password)),
    }
    return checks

print("\nPassword validation for 'Test123!':")
for check, passed in validate_password("Test123!").items():
    status = "✓" if passed else "✗"
    print(f"  {status} {check}")

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