Regex for beginners often feels intimidating at first glance: a string like ^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$ looks like noise until you know what each part means. Regular expressions, also called regex or regexp, are patterns used to match, find, and replace text based on rules rather than exact strings. They appear in text editors, programming languages, command-line tools, form validation, log parsers, and data cleaning scripts. Once you learn the core concepts, you will use regex constantly.
The reputation for being unreadable is partly earned: a regex written by someone else with no comments is genuinely difficult to parse. But the syntax is small and learnable. Most of what you will use in real work comes down to about fifteen concepts, and this guide covers all of them with pattern examples showing exactly what each one matches and does not match.
The basics: literals and metacharacters
Most characters in a regex match themselves exactly. The pattern cat matches the letters c, a, t in that sequence anywhere in a string. These are called literal characters. Some characters have special meaning and are called metacharacters: . * + ? ^ $ { } [ ] | ( ) and \. To match a metacharacter literally, escape it with a backslash: \. matches an actual period, not 'any character'.
Pattern: cat
Matches: "I have a cat", "concatenate", "cats"
No match: "dog", "CAT" (case-sensitive by default)
Pattern: 3\.14
Matches: "3.14"
No match: "3X14" (escaped dot matches only a literal period)The dot: match any character
A period (.) matches any single character except a newline. It is one of the most used metacharacters and one of the most overused. Use it when you genuinely mean 'any character here', and prefer a more specific pattern when you can.
Pattern: c.t
Matches: "cat", "cut", "c1t", "c@t", "c t"
No match: "ct" (nothing between c and t), "coat" (two chars between)Character classes: [abc]
Square brackets define a character class that matches any one character from the set. A hyphen inside the brackets indicates a range. A caret at the start negates the class.
Pattern: [aeiou]
Matches: any single vowel
Pattern: [0-9]
Matches: any single digit (same as \d)
Pattern: [a-zA-Z]
Matches: any uppercase or lowercase letter
Pattern: [^0-9]
Matches: any character that is NOT a digitShorthand character classes
These shorthand notations are the ones you will type most often:
- •\d: any digit, same as [0-9]
- •\D: any non-digit, same as [^0-9]
- •\w: any word character: letters, digits, underscore. Same as [a-zA-Z0-9_]
- •\W: any non-word character
- •\s: any whitespace: space, tab, newline
- •\S: any non-whitespace character
Quantifiers: how many times
Quantifiers follow a character or class and control how many times it must appear to count as a match.
- •*: zero or more times
- •+: one or more times
- •?: zero or one time (makes the preceding element optional)
- •{n}: exactly n times
- •{n,}: n or more times
- •{n,m}: between n and m times (inclusive)
Pattern: \d+
Matches: "1", "42", "2026" (one or more digits)
No match: "" (empty), "abc"
Pattern: \d{4}
Matches: "2026", "1999" (exactly four digits)
No match: "26", "12345"
Pattern: colou?r
Matches: "color", "colour" (u is optional)
No match: "colouur"Anchors: position in the string
Anchors do not match characters: they match positions. This makes them essential for validating that a pattern covers the whole input rather than just part of it.
Pattern: ^hello
Matches: "hello world" (starts with hello)
No match: "say hello" (hello not at start)
Pattern: world$
Matches: "hello world" (ends with world)
No match: "world peace" (world not at end)
Pattern: ^\d{5}$
Matches: "90210", "10001" (exactly five digits, nothing else)
No match: "9021" (too short), "902100" (too long)
Pattern: \bcat\b
Matches: "cat", "my cat is here" (cat as a whole word)
No match: "catch", "concatenate" (cat inside a larger word)Groups and alternation
Parentheses group parts of a pattern together. Groups serve two purposes: applying a quantifier to multiple characters at once, and capturing the matched text for use in replacements. The pipe character | means OR.
Pattern: (ab)+
Matches: "ab", "abab", "ababab"
No match: "a", "b", "ba"
Pattern: cat|dog
Matches: "cat", "dog"
No match: "bird"
Pattern: (cat|dog)s?
Matches: "cat", "cats", "dog", "dogs"
No match: "bird"Greedy vs lazy matching
By default, quantifiers are greedy: they match as much text as possible. Adding ? after a quantifier makes it lazy: it matches as little as possible. Greedy vs lazy is one of the most common sources of unexpected results for beginners.
Input: "<b>bold</b> and <i>italic</i>"
Pattern: <.+> (greedy)
Matches: "<b>bold</b> and <i>italic</i>" (one big match from first < to last >)
Pattern: <.+?> (lazy)
Matches: "<b>", "</b>", "<i>", "</i>" (each tag separately)Flags that change how a pattern works
- •i (case-insensitive): /cat/i matches Cat, CAT, cAt — use when case does not matter
- •g (global): find all matches in the string, not just the first — essential for replace-all operations
- •m (multiline): ^ and $ match the start and end of each line, not just the whole string
- •s (dotAll): the dot . matches newline characters too, not just non-newline characters
Real-world regex patterns with examples
These are the patterns that come up repeatedly in real work. Each one is shown with an explanation of its parts and examples of what it matches.
Email address validation
Pattern: [\w.+-]+@[\w-]+\.[a-zA-Z]{2,}
Breakdown:
[\w.+-]+ one or more word chars, dots, plus signs, or hyphens (local part)
@ literal @ symbol
[\w-]+ one or more word chars or hyphens (domain name)
\. literal dot
[a-zA-Z]{2,} two or more letters (TLD: com, org, co, etc.)
Matches: [email protected], [email protected]
No match: @example.com, user@, [email protected]Date in YYYY-MM-DD format
Pattern: ^\d{4}-\d{2}-\d{2}$
Breakdown:
^ start of string
\d{4} exactly four digits (year)
- literal hyphen
\d{2} exactly two digits (month)
- literal hyphen
\d{2} exactly two digits (day)
$ end of string
Matches: 2026-06-24, 1999-12-31
No match: 24-06-2026, June 24 2026, 2026-6-24URL matching
Pattern: https?:\/\/[\w\-._~:\/?#@!$&'()*+,;=%]+
Breakdown:
https? matches http or https (s is optional)
:\/\/ literal ://
[\w...]+ one or more valid URL characters
Matches: https://example.com, http://sub.domain.org/path?q=1
No match: ftp://example.com, example.com (no protocol)Phone number (flexible)
Pattern: [+]?[(]?[0-9]{1,4}[)]?[\s./0-9]{6,14}
Matches: +1 (555) 123-4567, 0044 20 7123 4567, 555.123.4567
Note: phone number formats vary enormously by country.
This pattern accepts most common formats without being so strict
that it rejects valid numbers.IPv4 address
Pattern: \b(?:\d{1,3}\.){3}\d{1,3}\b
Breakdown:
\b word boundary (no partial matches inside longer numbers)
(?:\d{1,3}\.) one to three digits followed by a dot, grouped but not captured
{3} repeated exactly three times
\d{1,3} final octet (one to three digits)
\b word boundary
Matches: 192.168.1.1, 10.0.0.255
No match: 999.999.999.999 (syntactically matches but IPs above 255 are invalid — use code validation for strict checking)Find duplicate words
Pattern: \b(\w+)\s+\1\b
Breakdown:
(\w+) captures one or more word characters into group 1
\s+ one or more whitespace characters
\1 backreference: must match exactly what group 1 captured
Matches: "the the", "is is", "very very important"
No match: "the cat", "this is" (different words)Common beginner mistakes
- •Forgetting to escape the dot: writing . when you mean a literal period. Use \. for a literal dot.
- •Using .* when \w+ or a character class would be more accurate. Greedy .* can match far more than intended.
- •Not anchoring validation patterns: the pattern \d{5} matches '90210' inside 'ABC902100XYZ'. Add ^ and $ to validate the whole string.
- •Forgetting the global flag when replacing all occurrences. Without g, only the first match is replaced.
- •Case sensitivity: regex is case-sensitive by default. Add the i flag when matching user input that could be uppercase or lowercase.
- •Testing only the happy path: always test what the pattern should NOT match, not just what it should match.
How to test a regex before using it in code
Writing a regex in your head and pasting it directly into production code is asking for edge-case failures. Always test first. AlteredIdea's Regex Tester lets you type a pattern and test it against sample input with live highlighting: matches are highlighted as you type, and the results panel shows each match, its position, and any captured groups.
A good test session covers three categories: inputs that should match and do, inputs that should not match and do not, and boundary cases (empty string, single character, maximum length, special characters). Running all three gives you confidence the pattern is correct before you ship it.
Quick reference
| Symbol | Meaning | Example |
|---|---|---|
| . | Any character except newline | c.t matches cat, cut, c1t |
| \d | Any digit | \d{4} matches 2026 |
| \w | Any word character | \w+ matches hello, user_1 |
| \s | Any whitespace | \s+ matches spaces, tabs |
| ^ | Start of string | ^Hello matches string starting with Hello |
| $ | End of string | world$ matches string ending with world |
| \b | Word boundary | \bcat\b matches cat but not catch |
| + | One or more | \d+ matches 1, 42, 2026 |
| * | Zero or more | colou*r matches color, colour, colouuur |
| ? | Zero or one (optional) | colou?r matches color and colour |
| {n} | Exactly n times | \d{4} matches exactly 4 digits |
| [abc] | Any one of these characters | [aeiou] matches any vowel |
| [^abc] | Any character NOT in set | [^0-9] matches non-digits |
| (abc) | Group and capture | (cat|dog) captures cat or dog |
| | | OR | cat|dog matches cat or dog |
AlteredIdea's Regex Tester gives you live match highlighting, captured group details, and a built-in reference panel. Test your patterns against real input before using them in code. No account needed.