Regular Expressions: A Practical Guide for Beginners
Regular expressions (regex) are one of the most powerful tools in a developer's toolkit, yet they often seem intimidating to beginners. This guide will teach you regex from scratch using practical, real-world examples. Follow along with our regex tester to try each pattern.
What Are Regular Expressions?
A regular expression is a pattern that describes a set of strings. Think of it as a search query on steroids. Instead of searching for an exact word, you can search for patterns like "any email address" or "a phone number in any format".
Basic Building Blocks
Literal Characters
The simplest regex is just literal text. The pattern hello matches the text "hello" exactly. Most characters match themselves.
The Dot (.)
The dot matches any single character except a newline:
Pattern: h.t
Matches: "hat", "hot", "hit", "h@t", "h t"
Doesn't match: "ht", "hoot"
Character Classes [ ]
Square brackets define a set of characters to match:
Pattern: [aeiou] — any vowel
Pattern: [0-9] — any digit
Pattern: [a-zA-Z] — any letter
Pattern: [^0-9] — anything that's NOT a digit
Shorthand Character Classes
\d — any digit (same as [0-9])
\w — any word character (same as [a-zA-Z0-9_])
\s — any whitespace (space, tab, newline)
\D — any non-digit
\W — any non-word character
\S — any non-whitespace
Quantifiers: How Many?
Quantifiers specify how many times a pattern should match:
* — 0 or more times
+ — 1 or more times
? — 0 or 1 time (optional)
{3} — exactly 3 times
{2,5} — between 2 and 5 times
{3,} — 3 or more times
Examples
Pattern: \d{3}-\d{4}
Matches: "555-1234", "800-5678"
Pattern: colou?r
Matches: "color", "colour"
Pattern: \w+@\w+\.\w+
Matches: basic email-like patterns
Anchors: Position Matters
^ — start of string (or line with /m flag)
$ — end of string (or line with /m flag)
\b — word boundary
Example: ^\d{5}$ matches a string that is exactly 5 digits (like a US ZIP code).
Groups and Alternation
Capturing Groups ( )
Parentheses create groups that can be referenced later:
Pattern: (\d{4})-(\d{2})-(\d{2})
Input: "2026-02-09"
Group 1: "2026"
Group 2: "02"
Group 3: "09"
Alternation |
The pipe acts as an OR operator:
Pattern: cat|dog
Matches: "cat" or "dog"
Pattern: (Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day
Matches: any day of the week
Real-World Patterns
Email Validation (Simple)
[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z]{2,}
URL Matching
https?://[^\s]+
Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
IPv4 Address
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Hex Color Code
#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})\b
Tips for Writing Better Regex
- Start simple and build up gradually
- Use a regex tester to verify patterns against test data
- Be specific rather than overly broad —
\d{3}is better than\d+when you expect exactly 3 digits - Use non-capturing groups
(?:...)when you don't need the captured value - Comment complex patterns using the verbose flag where supported
- Remember that regex is greedy by default —
.*matches as much as possible. Use.*?for lazy matching
Next Steps
The best way to learn regex is practice. Head over to our regex tester and try matching different patterns against your own text. Start with simple patterns and gradually work up to complex ones.