Back to Blog
Developer Tools8 min read·March 10, 2026

Regex Explained for Beginners with Practical Examples

Learn regular expressions from scratch. This beginner-friendly regex guide covers patterns, syntax, and real-world examples for developers.

What Is a Regular Expression?

A regular expression (regex) is a pattern used to match, search, and manipulate text. Think of it as a powerful "find and replace" on steroids. Developers use regex for form validation, data extraction, log parsing, and much more.

Why Learn Regex?

Regex is used in virtually every programming language — JavaScript, Python, Java, Go, PHP, and more. Mastering it lets you:

  • Validate user input — email, phone numbers, URLs
  • Extract data from logs, HTML, or CSV files
  • Search and replace complex patterns in code
  • Parse text from APIs or files
  • Write cleaner code with fewer lines

Basic Regex Syntax

Literal Characters

The simplest regex matches exact text. The pattern hello matches the string "hello" in any text.

Character Classes

  • [abc] — matches "a", "b", or "c"
  • [0-9] — matches any digit
  • [a-zA-Z] — matches any letter
  • [^abc] — matches anything EXCEPT "a", "b", or "c"

Quantifiers

  • * — zero or more times
  • + — one or more times
  • ? — zero or one time
  • {3} — exactly 3 times
  • {2,5} — between 2 and 5 times

Special Characters

  • . — matches any character (except newline)
  • \d — matches any digit (same as [0-9])
  • \w — matches any word character (letters, digits, underscore)
  • \s — matches any whitespace
  • ^ — start of string
  • $ — end of string

Practical Regex Examples

Validate an Email Address

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Match a Phone Number (US format)

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

Extract URLs from Text

https?:\/\/[^\s]+

Match an IP Address

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

Validate a Strong Password

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Common Regex Mistakes

  • Forgetting to escape special characters — dots, brackets, and backslashes need escaping
  • Being too greedy — use .*? instead of .* for non-greedy matching
  • Not anchoring patterns — use ^ and $ to match the full string
  • Overcomplicating patterns — simpler is always better
  • Test Your Regex Online

    Use our Regex Tester tool to build and test patterns in real time. See matches highlighted instantly, get explanations for your patterns, and debug with ease — all in your browser.

    Regex Cheat Sheet

    PatternDescription

    \dAny digit
    \wAny word character
    \sAny whitespace
    .Any character
    *Zero or more
    +One or more
    ?Optional
    ^Start of string
    $End of string
    ()Grouping
    \OR operator

    Start Practicing

    The best way to learn regex is by doing. Open our Regex Tester and start experimenting with patterns. Try matching emails, URLs, and phone numbers from sample text.

    #regex#regular expressions#regex tester#developer guide#pattern matching

    Try Our Free Online Tools

    100+ free tools for developers, designers, and everyone. No sign-up required.

    Browse All Tools