Skip to content

    Regular Expressions

    Python · Intermediate

    Definition

    The re module matches text patterns such as emails, numbers and dates.

    Powerful for validation and extraction.

    Diagram

    \d+ digits\w+ words^…$ whole string

    Flowchart

    Write patternre.search / findall / subUse groups to extractCompile for reuse

    Example: Extract numbers

    import re
    print(re.findall(r'\d+', 'Order 12 has 3 items'))

    Output: ['12', '3']

    Key points

    • Use raw strings r'...'
    • Avoid regex for full HTML parsing

    Interview questions

    match vs search?

    match checks only the start; search scans the whole string.

    More Python topics