Regex for beginners: the first 10 patterns to learn
New to regular expressions? Start here. Ten small patterns, each one a building block: plain text, the dot, character classes, \d, quantifiers, anchors, alternation, groups, escaping and word boundaries. Every example is live, so you can edit it and see what changes.

· 9 min read
/^c.t$/
match
- cat
- cot
- cut
- c9t
spare
- ct
- coat
- scat
- cats
A regular expression, or regex, is a small pattern that describes text. "Three digits." "A word that starts with a capital." "Anything ending in .pdf." Code editors, spreadsheets, search tools and every programming language understand them.
They look cryptic because every character is short, not because the ideas are hard. Ten building blocks cover most of what people use day to day, and this post introduces them one at a time. Every example is live: change the pattern and the lines update.
01Plain text matches itself
Most characters just mean themselves. The pattern cat finds the letters c, a, t in a row, anywhere:
Matching is case-sensitive by default, so CAT doesn't count.
02The dot: any one character
. matches exactly one character, whatever it is:
ct fails because there's nothing between the c and the t. coat fails because there are two.
03A set of characters: [ ]
Square brackets mean "one character from this set":
A hyphen inside makes a range: [a-z] is any lowercase letter, [0-9] any digit. More on sets in character classes.
04Shorthands: \d, \w, \s
Some sets have short names. \d is a digit, \w a word character (letter, digit or underscore), \s a space or other whitespace:
05How many: + * ? {n}
A quantifier after something says how many times it repeats:
| Quantifier | Means |
|---|---|
+ |
one or more |
* |
zero or more |
? |
zero or one (optional) |
{3} |
exactly 3 |
{2,4} |
2 to 4 |
06Start and end: ^ and $
^ means "the start of the text" and $ means "the end". Use both when the whole text has to match, not just part of it:
Without them, \d+ would happily find the digits inside 2026 AD. More in anchors and multiline mode.
07Either-or: |
| means "or":
The parentheses keep the ^ and $ around both choices.
08Groups: ( )
Parentheses group several characters so a quantifier applies to all of them:
09Escaping special characters: \
Characters like . ? + * ( and $ have special meanings. To match one literally, put a backslash before it:
10Whole words: \b
\b marks the edge of a word. Wrap a word in \b … \b to match it on its own, not inside a longer word:
11Putting them together
A US-style phone number like 555-0199 or 555 0199: three digits, an optional hyphen or space, four digits, and nothing else:
/^\d{3}[- ]?\d{4}$/Should match3/3
- 555-0199
- 555 0199
- 5550199
Should not3/3
- 555--0199
- 55-0199
- call 555-0199
That's pattern 3, 4, 5 and 6 working together. Most real patterns are just these blocks combined.
12Try one
Your first hunt: catch the monsters, spare the innocents.
Catch the words that end in ing.
Targets · 0/5 caught
- runningtarget
- singtarget
- thingtarget
- bringtarget
- kingtarget
Innocents · 0 hit
- ingotinnocent
- singerinnocent
- angleinnocent
- kingsinnocent
- ring a bellinnocent
Type a regex — the board updates as you go.
13Checklist
- Plain characters match themselves.
.matches any one character. [abc]is one character from a set.\d,\w,\sare shorthands.+ * ? {n}say how many.^and$pin the start and end.|is "or".( )groups.\makes a special character literal.\bmarks a word edge.
14Keep practising
- Shorthand patrol is the first practice pack.
- The regex guide explains everything here in more depth, and today's daily hunt is a good next step.