What Are Regular Expressions?
Regular expressions (regex) are patterns that describe text. They're used for searching, matching, and manipulating strings in virtually every programming language.
Basic Building Blocks
| Pattern | Matches | Example |
|---|---|---|
. | Any character | c.t → cat, cut, c9t |
\d | Any digit | \d\d → 42, 99, 01 |
\w | Word character | \w+ → hello, abc123 |
\s | Whitespace | \s → space, tab, newline |
^ | Start of string | ^Hello → "Hello world" |
$ | End of string | world$ → "Hello world" |
| Pattern | Meaning | Example |
|---|---|---|
| 0 or more | abc → ac, abc, abbc |
+ | 1 or more | ab+c → abc, abbc |
? | 0 or 1 | colou?r → color, colour |
{3} | Exactly 3 | \d{3} → 123, 456 |
{2,4} | Between 2 and 4 | \d{2,4} → 12, 123, 1234 |
^[\w.-]+@[\w.-]+\.\w{2,}$
Phone number (US):
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
URL:
https?://[\w.-]+(/[\w.-])
Hex color code:
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Groups and Alternatives
(abc)— capturing group(?:abc)— non-capturing groupa|b— matches a OR b[abc]— character class: a, b, or c[^abc]— NOT a, b, or c[a-z]— range: any lowercase letter
Testing Your Regex
The Regex Tester tool lets you:
- Write regex patterns with instant feedback
- See all matches highlighted in your test text
- Understand what each part of your pattern does
- Test edge cases before using in production
Common Mistakes
., *, ?, (, ) have special meaning. grabs as much as possible; use .? for lazy matching^ and $, patterns match anywhere in the stringregex
regular expressions
developer
pattern matching



