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 regex

Example:

file_001.txt

Breakdown:

file_      literal text
001        three digits
.txt       literal extension

Regex:

^file_\d{3}\.txt$

Anchors

Match beginning and end of text.

^
$

Example:

^cat$

Matches:

cat

Does NOT match:

wildcat
cat123

Character Classes

Match one character from a set.

[abc]

Matches:

a
b
c

Ranges:

[a-z]
[A-Z]
[0-9]

Negation:

[^0-9]

Meaning:

any character except a digit

Common Shortcuts

\d    digit
\w    word character
\s    whitespace
 
\D    not digit
\W    not word character
\S    not whitespace

Examples:

\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 b

Matches:

ab
abb
abbb

NOT:

abab

Groups

Parentheses create a unit.

(ab)+

Matches:

ab
abab
ababab

The + applies to the whole group.

Without grouping:

ab+

The + applies only to b.


Alternation (OR)

cat|dog

Matches:

cat
dog

Grouped:

(cats?|dogs?)

Matches:

cat
cats
dog
dogs

Escaping Special Characters

Special characters:

. * + ? ^ $ \ | ( ) [ ] { }

To match them literally:

\.
\(
\)
\[
\]

Example:

\.txt

Matches:

.txt

Dot (.)

.

Matches any single character.

Example:

a.c

Matches:

abc
a1c
a-c

Lookahead (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 12

Password Regex Structure

Think of it as:

Start
 
Check lowercase
Check uppercase
Check digit
Check special
 
Consume 12+ characters
 
End

Equivalent 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:

  1. Write valid examples.

  2. Write invalid examples.

  3. Break valid examples into pieces.

  4. Convert each piece into regex.

  5. Add anchors (^ and $) if matching the entire string.

  6. Verify against invalid examples.

Example:

report_2026-06-22.txt

Pieces:

report_
YYYY
-
MM
-
DD
.txt

Regex:

^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.