Goal
Learn to formulate regex patterns from requirements instead of trial-and-error with online tools.
Mental Model
A regex is a language for describing text patterns.
Think:
Requirement
↓
Break into pieces
↓
Describe each piece
↓
Combine into a regexExample:
file_001.txtBreakdown:
file_ literal text
001 three digits
.txt literal extensionRegex:
^file_\d{3}\.txt$Anchors
Match beginning and end of text.
^
$Example:
^cat$Matches:
catDoes NOT match:
wildcat
cat123Character Classes
Match one character from a set.
[abc]Matches:
a
b
cRanges:
[a-z]
[A-Z]
[0-9]Negation:
[^0-9]Meaning:
any character except a digitCommon Shortcuts
\d digit
\w word character
\s whitespace
\D not digit
\W not word character
\S not whitespaceExamples:
\d+
\w+
\s+Quantifiers
Describe how many times something appears.
a*Zero or more.
a+One or more.
a?Zero or one.
a{3}Exactly 3.
a{2,5}Between 2 and 5.
a{2,}At least 2.
Important Rule
Quantifiers apply to the nearest atom on their left.
Example:
ab+Means:
a
followed by
one or more bMatches:
ab
abb
abbbNOT:
ababGroups
Parentheses create a unit.
(ab)+Matches:
ab
abab
abababThe + applies to the whole group.
Without grouping:
ab+The + applies only to b.
Alternation (OR)
cat|dogMatches:
cat
dogGrouped:
(cats?|dogs?)Matches:
cat
cats
dog
dogsEscaping Special Characters
Special characters:
. * + ? ^ $ \ | ( ) [ ] { }To match them literally:
\.
\(
\)
\[
\]Example:
\.txtMatches:
.txtDot (.)
.Matches any single character.
Example:
a.cMatches:
abc
a1c
a-cLookahead (Introduction)
Password regex introduced lookaheads.
Example:
(?=.*[a-z])Meaning:
Verify that a lowercase letter exists somewhere ahead.Important:
-
Consumes no characters.
-
Acts like a checkpoint.
-
Engine returns to the same position after checking.
Example password regex:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{12,}$Requirements:
At least one lowercase
At least one uppercase
At least one digit
At least one special character
Minimum length 12Password Regex Structure
Think of it as:
Start
Check lowercase
Check uppercase
Check digit
Check special
Consume 12+ characters
EndEquivalent layout:
^
(?=.*[a-z])
(?=.*[A-Z])
(?=.*\d)
(?=.*[^A-Za-z0-9])
.{12,}
$Key Insight
This:
.{12,}Applies only to:
.NOT to all preceding lookaheads.
Reason:
Quantifiers always attach to the nearest atom/group on the left.To repeat multiple items together:
(abc){12,}Use a group.
Regex Formulation Recipe
Given a requirement:
-
Write valid examples.
-
Write invalid examples.
-
Break valid examples into pieces.
-
Convert each piece into regex.
-
Add anchors (
^and$) if matching the entire string. -
Verify against invalid examples.
Example:
report_2026-06-22.txtPieces:
report_
YYYY
-
MM
-
DD
.txtRegex:
^report_\d{4}-\d{2}-\d{2}\.txt$Lessons Learned
-
Regex is a language, not magic.
-
Quantifiers apply to the nearest atom/group.
-
Parentheses create groups.
-
Anchors control full-string matching.
-
Character classes describe allowed characters.
-
Lookaheads are checks, not matches.
-
Most regexes can be designed on paper before testing.