Curly Braces {} ,Pipe (|) Metacharacters

Curly Braces {} in Python Regex

Curly braces {} are used to specify exact quantity of the preceding character or group. They define how many times something should appear.

Basic Syntax:

  • {n} – exactly n times
  • {n,} – n or more times
  • {n,m} – between n and m times (inclusive)

Example 1: Exact Number of Digits

python

import re

text = "Zip codes: 12345, 9876, 123, 123456, 90210"

# Match exactly 5 digits
pattern = r"\d{5}"  # Exactly 5 digits
matches = re.findall(pattern, text)
print("Exactly 5 digits:", matches)  # Output: ['12345', '90210']

# Match 4 or 5 digits
pattern = r"\d{4,5}"  # 4 to 5 digits
matches = re.findall(pattern, text)
print("4-5 digits:", matches)  # Output: ['12345', '9876', '90210']

# Match 3 or more digits
pattern = r"\d{3,}"  # 3 or more digits
matches = re.findall(pattern, text)
print("3+ digits:", matches)  # Output: ['12345', '9876', '123', '123456', '90210']

Example 2: Exact Number of Letters

python

import re

text = "Words: cat, dog, elephant, bat, ant, butterfly"

# Match exactly 3 letters
pattern = r"\b[a-z]{3}\b"  # Exactly 3-letter words
matches = re.findall(pattern, text)
print("3-letter words:", matches)  # Output: ['cat', 'dog', 'bat', 'ant']

# Match 4-6 letters
pattern = r"\b[a-z]{4,6}\b"  # 4 to 6 letter words
matches = re.findall(pattern, text)
print("4-6 letter words:", matches)  # Output: ['elephant', 'butterfly'] - Wait, this doesn't work right!

# Fixed version - elephant and butterfly are too long
pattern = r"\b[a-z]{4,6}\b"  # 4 to 6 letter words
matches = re.findall(pattern, text)
print("4-6 letter words:", matches)  # Output: [] - No words in this range

# Let's try with different text
text2 = "Words: tree, house, computer, car, elephant"
matches = re.findall(r"\b[a-z]{4,6}\b", text2)
print("4-6 letter words:", matches)  # Output: ['house', 'computer', 'car']

Example 3: Phone Number Patterns

python

import re

text = "Phones: 555-1234, 1-800-555-1234, 123-4567, 5551234"

# Match exactly XXX-XXXX pattern
pattern = r"\d{3}-\d{4}"  # 3 digits, hyphen, 4 digits
matches = re.findall(pattern, text)
print("XXX-XXXX format:", matches)  # Output: ['555-1234', '123-4567']

# Match phone numbers with area code
pattern = r"\d{3}-\d{3}-\d{4}"  # 3-3-4 digits with hyphens
matches = re.findall(pattern, text)
print("XXX-XXX-XXXX format:", matches)  # Output: ['800-555-1234']

# Match numbers with 7+ digits
pattern = r"\d{7,}"  # 7 or more digits
matches = re.findall(pattern, text)
print("7+ digits:", matches)  # Output: ['5551234']

Example 4: Complex Pattern with Groups

python

import re

text = "Dates: 2023-01-15, 1999-12-25, 2020-2-5, 2000-10-01"

# Match YYYY-MM-DD format with exactly 2 digits for month and day
pattern = r"\d{4}-\d{2}-\d{2}"  # 4-2-2 digits
matches = re.findall(pattern, text)
print("YYYY-MM-DD format:", matches)  # Output: ['2023-01-15', '1999-12-25', '2000-10-01']

# Match with 1-2 digits for month and day
pattern = r"\d{4}-\d{1,2}-\d{1,2}"  # 4 digits, then 1-2, then 1-2
matches = re.findall(pattern, text)
print("Flexible date format:", matches)  # Output: ['2023-01-15', '1999-12-25', '2020-2-5', '2000-10-01']

Example 5: Password Strength Checker

python

import re

passwords = ["abc123", "password", "P@ssw0rd", "Strong123!", "weak"]

# Check for passwords with at least 8 characters including at least 2 digits
pattern = r"^(?=.*\d.*\d).{8,}$"

for pwd in passwords:
    if re.match(pattern, pwd):
        print(f"'{pwd}' - Strong password")
    else:
        print(f"'{pwd}' - Weak password")

# Output:
# 'abc123' - Weak password (too short)
# 'password' - Weak password (no digits)
# 'P@ssw0rd' - Strong password
# 'Strong123!' - Strong password  
# 'weak' - Weak password (too short, no digits)

Key Points:

  • {n} – exactly n repetitions
  • {n,} – n or more repetitions
  • {n,m} – between n and m repetitions (inclusive)
  • Works with characters, character classes, and groups
  • Useful for validating specific patterns (phone numbers, zip codes, dates)
  • More precise than * (0 or more) or + (1 or more)

The Pipe (|) in Python Regex

The pipe | is the OR operator in regular expressions. It allows you to match one pattern OR another pattern.

Basic Syntax:

  • pattern1|pattern2 – matches pattern1 OR pattern2
  • Can be used with multiple alternatives: pattern1|pattern2|pattern3

Example 1: Matching Multiple Words

python

import re

text = "I have a cat, a dog, and a parrot as pets. I also like birds."

# Match any of these animals
pattern = r"cat|dog|parrot|bird"
matches = re.findall(pattern, text)
print("Animals found:", matches)
# Output: ['cat', 'dog', 'parrot', 'bird']

Explanation: The regex looks for either “cat”, “dog”, “parrot”, or “bird” in the text.


Example 2: Different Date Formats

python

import re

text = "Dates: 2023-01-15, 15/01/2023, 01.15.2023, 2023/01/15"

# Match dates in different formats
pattern = r"\d{4}-\d{2}-\d{2}|\d{2}/\d{2}/\d{4}|\d{2}\.\d{2}\.\d{4}"
matches = re.findall(pattern, text)
print("Dates found:", matches)
# Output: ['2023-01-15', '15/01/2023', '01.15.2023']

Explanation: This matches dates in YYYY-MM-DD format OR DD/MM/YYYY format OR MM.DD.YYYY format.


Example 3: Phone Number Variations

python

import re

text = "Contact: 555-1234, (555) 123-4567, 1-800-555-1234, 5551234"

# Match different phone number formats
pattern = r"\(\d{3}\) \d{3}-\d{4}|\d{3}-\d{4}|\d{3}-\d{3}-\d{4}|\d{7}"
matches = re.findall(pattern, text)
print("Phone numbers found:", matches)
# Output: ['(555) 123-4567', '555-1234', '800-555-1234', '5551234']

Explanation: Matches phone numbers in various formats: (XXX) XXX-XXXX OR XXX-XXXX OR XXX-XXX-XXXX OR XXXXXXX.


Example 4: File Extensions

python

import re

text = "Files: document.pdf, image.jpg, data.xlsx, script.py, photo.png"

# Match different image file extensions
pattern = r"\.jpg|\.png|\.gif|\.bmp"
matches = re.findall(pattern, text)
print("Image extensions found:", matches)
# Output: ['.jpg', '.png']

Explanation: Finds files with .jpg, .png, .gif, or .bmp extensions.


Example 5: Combined with Groups

python

import re

text = "Colors: red, blue, green, yellow, purple, orange"

# Match specific color categories using groups
pattern = r"(red|blue|green)|(yellow|orange)|(purple|pink|brown)"
matches = re.findall(pattern, text)
print("Color matches:", matches)
# Output: [('red', '', ''), ('blue', '', ''), ('green', '', ''), ('', 'yellow', ''), ('', '', 'purple'), ('', 'orange', '')]

Explanation: Uses groups to categorize colors. Each tuple shows which alternative was matched.


Example 6: Case Insensitive Matching

python

import re

text = "The Quick Brown Fox jumps over the Lazy Dog"

# Match different case variations
pattern = r"quick|Quick|QUICK"
matches = re.findall(pattern, text)
print("Case variations:", matches)
# Output: ['Quick']

# Better approach with flags
matches = re.findall(r"quick", text, re.IGNORECASE)
print("Case insensitive:", matches)
# Output: ['Quick']

Important Notes:

  1. Order matters: The regex engine tries alternatives from left to right
  2. Use parentheses for clarity: (cat|dog) vs cat|dog
  3. Works with complex patterns: You can use | with any regex pattern
  4. First match wins: If multiple alternatives could match, the first one is chosen

python

# Example showing order matters
text = "catalog"
pattern1 = r"cat|category|catalog"
pattern2 = r"catalog|category|cat"

matches1 = re.findall(pattern1, text)  # ['cat']
matches2 = re.findall(pattern2, text)  # ['catalog']

print("Pattern1:", matches1)
print("Pattern2:", matches2)

The pipe | is extremely useful for creating flexible patterns that can handle multiple variations of the same concept!

Similar Posts

  • sqlite3 create table

    The sqlite3 module is the standard library for working with the SQLite database in Python. It provides an interface compliant with the DB-API 2.0 specification, allowing you to easily connect to, create, and interact with SQLite databases using SQL commands directly from your Python code. It is particularly popular because SQLite is a serverless database…

  • Functions Returning Functions

    Understanding Functions Returning Functions In Python, functions can return other functions, which is a powerful feature of functional programming. Basic Example python def outer(): def inner(): print(“Welcome!”) return inner # Return the inner function (without calling it) # Calling outer() returns the inner function f = outer() # f now refers to the inner function…

  • Python Statistics Module

    Python Statistics Module: Complete Methods Guide with Examples Here’s a detailed explanation of each method in the Python statistics module with 3 practical examples for each: 1. Measures of Central Tendency mean() – Arithmetic Average python import statistics as stats # Example 1: Basic mean calculation data1 = [1, 2, 3, 4, 5] result1 = stats.mean(data1) print(f”Mean of…

  • Type Conversion Functions

    Type Conversion Functions in Python 🔄 Type conversion (or type casting) transforms data from one type to another. Python provides built-in functions for these conversions. Here’s a comprehensive guide with examples: 1. int(x) 🔢 Converts x to an integer. Python 2. float(x) afloat Converts x to a floating-point number. Python 3. str(x) 💬 Converts x…

  • re.sub()

    Python re.sub() Method Explained The re.sub() method is used for searching and replacing text patterns in strings. It’s one of the most powerful regex methods for text processing. Syntax python re.sub(pattern, repl, string, count=0, flags=0) Example 1: Basic Text Replacement python import re text = “The color of the sky is blue. My favorite color is blue too.” #…

  • Linear vs. Scalar,Homogeneous vs. Heterogeneous 

    Linear vs. Scalar Data Types in Python In programming, data types can be categorized based on how they store and organize data. Two important classifications are scalar (atomic) types and linear (compound) types. 1. Scalar (Atomic) Data Types 2. Linear (Compound/Sequential) Data Types Key Differences Between Scalar and Linear Data Types Feature Scalar (Atomic) Linear (Compound) Stores Single…

Leave a Reply

Your email address will not be published. Required fields are marked *