Back to Home
    RegexRegex PCRE2Intermediate

    Regex

    Regular expression syntax, character classes, groups, lookaheads, and common patterns.

    8 min read
    regexpatternstextsearch

    Syntax

    2 topics

    Character Classes & Quantifiers

    bash
    # Character classes
    .        Any character except newline
    \d       Digit [0-9]
    \D       Non-digit
    \w       Word char [a-zA-Z0-9_]
    \W       Non-word char
    \s       Whitespace
    \S       Non-whitespace
    [abc]    a, b, or c
    [^abc]   Not a, b, or c
    [a-z]    Lowercase letter
    
    # Quantifiers
    *        0 or more (greedy)
    +        1 or more (greedy)
    ?        0 or 1
    {3}      Exactly 3
    {3,}     3 or more
    {3,6}    Between 3 and 6
    *?  +?  ?? Non-greedy variants
    
    # Anchors
    ^        Start of string/line
    $        End of string/line
    \b       Word boundary

    💡 Use non-greedy *? when you want the shortest match

    ⚡ Word boundaries \b are great for whole-word matching

    Groups & Lookaheads

    bash
    # Groups
    (abc)         Capture group
    (?:abc)       Non-capturing group
    (?P<name>)   Named capture group
    
    # Alternation
    cat|dog       Matches cat or dog
    (jpg|png|gif) Image extensions
    
    # Lookahead / lookbehind
    (?=)    Positive lookahead
    (?!)    Negative lookahead
    (?<=)   Positive lookbehind
    (?<!)   Negative lookbehind
    
    # Example: price followed by USD
    \d+(?= USD)   Matches the number in '42 USD'
    
    # Example: not preceded by $
    (?<!\$)\d+    Matches bare numbers

    💡 Non-capturing groups (?:) are faster — use them when you don't need the match

    ⚡ Lookaheads/lookbehinds match without consuming characters

    Common Patterns

    1 topic

    Useful Regex Recipes

    bash
    # Email (basic)
    ^[\w.+-]+@[\w-]+\.[\w.-]+$
    
    # URL
    https?://[\w/:%#$&?()~.=+\-]+
    
    # IPv4
    ^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
    
    # Date YYYY-MM-DD
    ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
    
    # Hex color
    ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
    
    # Slug (URL-safe)
    ^[a-z0-9]+(?:-[a-z0-9]+)*$
    
    # Strong password (8+ chars, upper, lower, digit, special)
    ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

    ⚠️ Don't validate emails with regex in production — use a library

    💡 Test regex at regex101.com with real data before shipping

    Related Articles

    Background reading and deeper explanations for this sheet.

    Building Autonomous Agents Without Overengineering: A Practical Guide for Real-World Teams

    Autonomous agents can automate meaningful work, but many projects fail because teams overbuild too early. This practical guide shows you how to design, ship, and improve useful agents with simple patterns, clear guardrails, and real-world examples—without turning your stack into a science project.

    keyword overlap

    FastAPI Interview Questions: Practical Cheat Sheet for Developers

    Preparing for FastAPI interviews can feel overwhelming if you only memorize definitions. This practical cheat sheet helps you answer common FastAPI interview questions with confidence using real-world examples, production-ready patterns, and beginner-friendly explanations with depth.

    keyword overlap

    Memory in AI Agents: Vector DB vs Structured State (and When to Use Both)

    AI agents feel smart only when they can remember the right things at the right time. In this practical guide, you’ll learn the difference between vector database memory and structured state, when each approach shines, and how to combine them in real-world agent workflows. We’ll walk through concrete examples, implementation patterns, and pitfalls to avoid so you can build reliable, context-aware agents.

    keyword overlap

    System Design for 100 Million Requests/Day: Scalable Patterns Made Simple

    Designing for 100 million requests a day sounds intimidating, but it becomes manageable when you break traffic into layers and apply proven patterns. In this guide, you’ll learn the architecture, capacity math, and practical system design patterns—like caching, queue-based load leveling, and CQRS—with easy examples you can reuse.

    keyword overlap