Regular Expressions for Beginners: Patterns, Syntax, and Real Examples

Published June 3, 2026

Regular expressions (regex) are pattern-matching sequences that search, validate, and transform text. They're built into every programming language and text editor. Learning regex is one of the highest-leverage skills for any developer — a single pattern can replace dozens of lines of procedural code.


Basic Building Blocks


Literal characters match themselves: cat matches "cat" in "concatenate"


The dot (.) matches any single character: c.t matches "cat", "cot", "c9t"


Character classes [...] match one character from a set:

  • [aeiou] matches any vowel
  • [0-9] matches any digit
  • [A-Za-z] matches any letter
  • [^0-9] matches any non-digit (^ negates inside brackets)

  • Shorthand classes:

  • \d = [0-9] (digit)
  • \w = [A-Za-z0-9_] (word character)
  • \s = space, tab, newline (whitespace)
  • \D, \W, \S = negated versions

  • Quantifiers (How Many)


  • * = zero or more: ab*c matches "ac", "abc", "abbc"
  • + = one or more: ab+c matches "abc", "abbc" but NOT "ac"
  • ? = zero or one: colou?r matches "color" and "colour"
  • {3} = exactly 3: \d{3} matches "123"
  • {2,4} = between 2 and 4: \d{2,4} matches "12", "123", "1234"

  • Anchors (Where to Match)


  • ^ = start of string (or line with m flag)
  • $ = end of string (or line with m flag)
  • \b = word boundary

  • Examples:

  • ^Hello matches "Hello world" but not "Say Hello"
  • world$ matches "Hello world" but not "world peace"
  • \bcat\b matches "the cat sat" but not "concatenate"

  • Groups and Capturing


    Parentheses (...) create groups that:

    1. Group elements for quantifiers: (ab)+ matches "ab", "abab", "ababab"

    2. Capture matched text for extraction or backreference


    Example: (\d{4})-(\d{2})-(\d{2}) matches "2026-06-15" and captures year, month, day separately.


    Named groups improve readability: (?\d{4})-(?\d{2})-(?\d{2})


    Practical Examples


    Email validation (simplified):

    [\w.]+@[\w]+\.[a-z]{2,}


    Phone number (US):

    \(?\d{3}\)?[-\s.]?\d{3}[-\s.]?\d{4}


    URL extraction:

    https?://[\w.-]+(?:/[\w./-]*)?


    Date (MM/DD/YYYY):

    (0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01])/\d{4}


    IP address:

    \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b


    Test all of these patterns instantly with our Regex Tester — paste the pattern and sample text to see matches highlighted in real-time.


    Common Mistakes


    1. Forgetting to escape special characters: . * + ? need backslashes for literal matching

    2. Greedy by default: .* grabs as much as possible. Use .*? for non-greedy

    3. Not using anchors: Without ^ and $, patterns match substrings anywhere

    4. Over-complicated patterns: Build incrementally and test at each step

    5. Forgetting the global flag: Without /g, only the first match is found


    When NOT to Use Regex


  • Parsing HTML/XML (use a proper parser)
  • Complex nested structures (regex can't count bracket depth)
  • When simple string methods (includes, startsWith, split) would work
  • When readability matters more than brevity
  • Try These Tools

    More Guides