Skip to main content
Editor

Python Regular Expressions: Pattern Matching from Basics to Advanced

Intermediate30 min7 exercises110 XP
0/7 exercises

Imagine you're working at a help desk and need to find every email address buried in thousands of support tickets. You could scan each line character by character, but that would take forever. Regular expressions let you describe the pattern of an email address, and Python finds every match for you.

Regular expressions (often called regex or regexp) are a mini-language for describing text patterns. Python's built-in re module gives you functions to search, extract, replace, and split text using these patterns.

In this tutorial, you'll learn to use re.search(), re.findall(), re.sub(), character classes, quantifiers, groups, and compiled patterns. By the end, you'll be able to validate input, extract data, and clean messy strings with confidence.

Why Do Regex Patterns Use Raw Strings?

Before diving into patterns, there's one important habit: always write regex patterns as raw strings by putting an r before the opening quote, like r'\d+'. Without the r, Python interprets backslashes as escape characters (like \n for newline) before the regex engine ever sees them.

Without raw string (risky)
import re
# \b is a backspace in normal strings!
pattern = '\\bcat\\b'
print(re.search(pattern, 'the cat sat'))
With raw string (correct)
import re
# r prefix keeps backslashes literal
pattern = r'\bcat\b'
print(re.search(pattern, 'the cat sat'))

How Do re.search() and re.match() Work?

re.search(pattern, string) scans the entire string and returns the first match it finds (or None if there's no match). re.match(pattern, string) only checks at the beginning of the string. Most of the time, re.search() is what you want.

search() vs match()
import re
text = 'Order #12345 placed on 2024-03-14'
# search() finds a match anywhere in the string
match = re.search(r'#(\d+)', text)
if match:
print(f'Found order: {match.group()}')
print(f'Order number: {match.group(1)}')
# match() only checks the beginning
result = re.match(r'Order', text)
print(f'match() at start: {result is not None}')
result2 = re.match(r'placed', text)
print(f'match() in middle: {result2 is not None}')
Output
Found order: #12345
Order number: 12345
match() at start: True
match() in middle: False

When re.search() finds a match, it returns a Match object. Call .group() to get the full match, or .group(1) to get the first captured group (the part inside parentheses). If there's no match, it returns None, so always check before calling .group().


What Are Character Classes and Quantifiers?

Character classes define which characters to match. Quantifiers define how many of them to match. Together, they form the backbone of every regex pattern.

Character classes and quantifiers in action
import re
# \d = digit, \w = word char, \s = whitespace
# + = one or more, * = zero or more, ? = zero or one
text = 'Call 555-1234 or 800-555-6789'
# \d{3} means exactly 3 digits
# \d{3,4} means 3 or 4 digits
phones = re.findall(r'\d{3}-\d{3,4}', text)
print(f'Phone numbers: {phones}')
# [A-Za-z] = any letter, [0-9] = any digit
email_text = 'Contact: alice@example.com or bob@test.org'
emails = re.findall(r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}', email_text)
print(f'Emails: {emails}')
Output
Phone numbers: ['555-1234', '800-555']
Emails: ['alice@example.com', 'bob@test.org']

Here's a quick reference for the most useful shorthand classes:

  • \d -- any digit (0-9)
  • \D -- any non-digit
  • \w -- any word character (letters, digits, underscore)
  • \W -- any non-word character
  • \s -- any whitespace (space, tab, newline)
  • \S -- any non-whitespace
  • . -- any character except newline

  • How Do You Find All Matches or Replace Text?

    re.findall() returns a list of all non-overlapping matches. re.sub() replaces every match with a new string. These two functions handle the most common regex tasks: extracting data and cleaning text.

    findall() and sub() examples
    import re
    log = 'ERROR: disk full at 14:23. WARNING: low memory at 14:25. ERROR: timeout at 14:30.'
    # findall returns a list of all matches
    errors = re.findall(r'ERROR: (\w+ \w+)', log)
    print(f'Errors: {errors}')
    times = re.findall(r'\d{2}:\d{2}', log)
    print(f'Times: {times}')
    # sub replaces matches with new text
    cleaned = re.sub(r'ERROR', 'ERR', log)
    print(f'Cleaned: {cleaned}')
    # sub with a function for dynamic replacement
    def censor(match):
    return '*' * len(match.group())
    result = re.sub(r'\d{2}:\d{2}', censor, log)
    print(f'Censored: {result}')
    Output
    Errors: ['disk full', 'timeout at']
    Times: ['14:23', '14:25', '14:30']
    Cleaned: ERR: disk full at 14:23. WARNING: low memory at 14:25. ERR: timeout at 14:30.
    Censored: ERROR: disk full at *****. WARNING: low memory at *****. ERROR: timeout at *****.

    Notice that when findall() uses capturing groups (parentheses), it returns only the captured part, not the full match. Without groups, it returns the full match. This is a common source of confusion.


    How Do Groups Let You Extract Specific Parts?

    Parentheses () create capturing groups that let you extract specific pieces of a match. Think of them as highlighting the parts you care about inside a larger pattern.

    Capturing groups and named groups
    import re
    date_text = 'Meeting on 2024-03-14 and 2024-12-25'
    # Three groups: year, month, day
    match = re.search(r'(\d{4})-(\d{2})-(\d{2})', date_text)
    if match:
    print(f'Full match: {match.group()}')
    print(f'Year: {match.group(1)}')
    print(f'Month: {match.group(2)}')
    print(f'Day: {match.group(3)}')
    # Named groups with ?P<name>
    match2 = re.search(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})', date_text)
    if match2:
    print(f'Named year: {match2.group("year")}')
    print(f'All groups: {match2.groups()}')
    Output
    Full match: 2024-03-14
    Year: 2024
    Month: 03
    Day: 14
    Named year: 2024
    All groups: ('2024', '03', '14')

    How Do You Split Strings with re.split()?

    Python's built-in str.split() only splits on a fixed separator. re.split() splits on a pattern, which is much more flexible. Need to split on commas, semicolons, and pipes all at once? Regex makes that easy.

    re.split() with patterns
    import re
    # Split on multiple delimiters
    data = 'apple,banana;cherry|date'
    fruits = re.split(r'[,;|]', data)
    print(fruits)
    # Split on one or more whitespace characters
    messy = ' hello world from python '
    words = re.split(r'\s+', messy.strip())
    print(words)
    # Limit the number of splits with maxsplit
    log_line = 'ERROR: disk full: cannot write: retry later'
    parts = re.split(r':\s*', log_line, maxsplit=1)
    print(parts)
    Output
    ['apple', 'banana', 'cherry', 'date']
    ['hello', 'world', 'from', 'python']
    ['ERROR', 'disk full: cannot write: retry later']

    The maxsplit parameter is handy when you only want to split at the first occurrence. In the log example above, we split only at the first colon so the rest of the message stays intact.


    What Are Some Common Regex Patterns?

    Here are patterns you'll use over and over. Each one solves a common real-world task:

    Common real-world regex patterns
    import re
    # Validate a simple username (3-16 alphanumeric + underscore)
    print(bool(re.fullmatch(r'\w{3,16}', 'cool_user42')))
    print(bool(re.fullmatch(r'\w{3,16}', 'ab')))
    # Extract all numbers (including decimals) from text
    text = 'Price: $12.99 and $3.50 plus 7% tax'
    numbers = re.findall(r'\d+\.?\d*', text)
    print(numbers)
    # Remove HTML tags from a string
    html = '<p>Hello <b>world</b></p>'
    clean = re.sub(r'<[^>]+>', '', html)
    print(clean)
    Output
    True
    False
    ['12.99', '3.50', '7']
    Hello world

    Practice Exercises

    Find a Phone Number
    Write Code

    Write a function find_phone(text) that uses re.search() to find the first phone number in the format XXX-XXX-XXXX in the given text. Return the phone number as a string, or "Not found" if no phone number exists.

    import re
    def find_phone(text):
    # Use re.search() with a pattern for XXX-XXX-XXXX
    pass
    # Test it
    print(find_phone('Call us at 555-867-5309 today!'))
    print(find_phone('No number here'))
    Extract All Hashtags
    Write Code

    Write a function extract_hashtags(text) that returns a list of all hashtags in the text. A hashtag starts with # followed by one or more word characters (letters, digits, or underscores). Return just the tag names without the # symbol.

    import re
    def extract_hashtags(text):
    # Use re.findall() with a group to capture the tag name
    pass
    result = extract_hashtags('Love #Python and #coding! #100DaysOfCode')
    print(result)
    Predict the Regex Output
    Predict Output

    What will this code print? Think carefully about greedy vs lazy matching and what findall returns with groups.

    import re
    text = '<b>bold</b> and <i>italic</i>'
    greedy = re.findall(r'<(.+)>', text)
    print(greedy)
    lazy = re.findall(r'<(.+?)>', text)
    print(lazy)
    Censor Credit Card Numbers
    Write Code

    Write a function censor_cards(text) that replaces all sequences of 4 groups of 4 digits separated by dashes (like 1234-5678-9012-3456) with ****-****-****-XXXX, where XXXX is the last 4 digits. Use re.sub() with a function or groups.

    import re
    def censor_cards(text):
    # Replace card numbers, keeping only last 4 digits
    pass
    print(censor_cards('Card: 1234-5678-9012-3456 and 9999-8888-7777-6666'))
    Fix the Email Validator
    Fix the Bug

    This email validator has two bugs. Find and fix them so it correctly validates that a string looks like an email address (one or more word characters/dots/hyphens, then @, then a domain, then a dot, then 2-4 letters).

    import re
    def is_valid_email(email):
    # BUG 1: Missing raw string prefix
    # BUG 2: Pattern doesn't anchor to start/end
    pattern = '[\w.-]+@[\w.-]+\.\w{2,4}'
    match = re.search(pattern, email)
    return match is not None
    print(is_valid_email('alice@example.com'))
    print(is_valid_email('not-an-email'))
    print(is_valid_email('!!!bad@email.com!!!'))
    Parse a Log Entry
    Write Code

    Write a function parse_log(entry) that extracts the timestamp, level, and message from a log entry formatted as "[HH:MM:SS] LEVEL: message". Return a dictionary with keys "time", "level", and "message". Use named groups. If the entry doesn't match, return None.

    import re
    def parse_log(entry):
    # Use named groups: (?P<name>pattern)
    pass
    result = parse_log('[14:23:05] ERROR: disk full')
    if result:
    print(result['time'])
    print(result['level'])
    print(result['message'])
    Clean and Normalize Whitespace
    Refactor

    Refactor this messy string-cleaning code to use re.sub(). The function should: (1) replace all runs of multiple spaces/tabs with a single space, and (2) strip leading and trailing whitespace. The current code uses multiple chained .replace() calls -- make it cleaner with one regex substitution plus .strip().

    import re
    def clean_text(text):
    # Current messy approach -- refactor to use re.sub()
    text = text.replace(' ', ' ')
    text = text.replace(' ', ' ')
    text = text.replace('\t', ' ')
    text = text.replace(' ', ' ')
    text = text.strip()
    return text
    print(clean_text(' Hello world\t from Python '))

    Harder problems

    More problems on this topic: Regular expressions

    Related Tutorials

    Was this tutorial helpful?