Basic Patterns

Learn regular expressions by seeing patterns, test cases, and match results side-by-side

Overview

Learn fundamental regex patterns including literal characters, wildcards, and basic quantifiers

01 Literal Match

The simplest regex pattern is a literal string. The pattern cat matches the exact sequence of characters "cat".

Key points:

  • Matches are case-sensitive by default
  • Finds the pattern anywhere in the string
  • The pattern "cat" matches within "concatenate"

Source: Regular-Expressions.info - Quick Start

Pattern: cat
Test Input
The cat sat on the mat.
I have a dog, not a cat.
Concatenate these strings.
CAT is not the same as cat.
Match Results
Line 1: "The cat sat on the mat."
Match 1: "cat"
Line 2: "I have a dog, not a cat."
Match 1: "cat"
Line 3: "Concatenate these strings."
Match 1: "cat"
Line 4: "CAT is not the same as cat."
Match 1: "cat"
All matches found: 4
Matches: ['cat', 'cat', 'cat', 'cat']

02 Dot Wildcard

The dot . is a wildcard that matches any single character except newline.

Pattern breakdown:

  • c - literal 'c'
  • . - any single character
  • t - literal 't'

This matches "cat", "cot", "cut", and even "c t" (with a space), but not "ct" (missing middle character) or "coat" (too many characters between c and t).

Source: Python re Documentation

Pattern: c.t
Test Input
cat
cot
cut
c t
ct
coat
Match Results
Line 1: "cat"
Match 1: "cat"
Line 2: "cot"
Match 1: "cot"
Line 3: "cut"
Match 1: "cut"
Line 4: "c t"
Match 1: "c t"
Line 5: "ct" - No match
Line 6: "coat" - No match
All matches found: 4
Matches: ['cat', 'cot', 'cut', 'c t']

03 Star Quantifier

The star * quantifier means "zero or more" of the preceding character.

Pattern breakdown:

  • c - literal 'c'
  • a* - zero or more 'a' characters
  • t - literal 't'

This matches "ct" (zero a's), "cat" (one a), "caat" (two a's), etc. It does not match "cbt" because the middle character must be 'a' if present.

Source: Regular-Expressions.info - Quantifiers

Pattern: ca*t
Test Input
ct
cat
caat
caaaat
cbt
Match Results
Line 1: "ct"
Match 1: "ct"
Line 2: "cat"
Match 1: "cat"
Line 3: "caat"
Match 1: "caat"
Line 4: "caaaat"
Match 1: "caaaat"
Line 5: "cbt" - No match
All matches found: 4
Matches: ['ct', 'cat', 'caat', 'caaaat']

04 Plus Quantifier

The plus + quantifier means "one or more" of the preceding character.

Pattern breakdown:

  • c - literal 'c'
  • a+ - one or more 'a' characters
  • t - literal 't'

Unlike *, this requires at least one 'a'. So "ct" does not match, but "cat", "caat", etc. do.

Source: Python Regex HOWTO

Pattern: ca+t
Test Input
ct
cat
caat
caaaat
Match Results
Line 1: "ct" - No match
Line 2: "cat"
Match 1: "cat"
Line 3: "caat"
Match 1: "caat"
Line 4: "caaaat"
Match 1: "caaaat"
All matches found: 3
Matches: ['cat', 'caat', 'caaaat']

05 Question Quantifier

The question mark ? quantifier means "zero or one" of the preceding character (makes it optional).

Pattern breakdown:

  • colou?r - matches both "color" and "colour"
  • The 'u' is optional

This is perfect for handling spelling variations between American and British English.

Source: Regular-Expressions.info - Optional Items

Pattern: colou?r
Test Input
color
colour
colouur
color vs colour
Match Results
Line 1: "color"
Match 1: "color"
Line 2: "colour"
Match 1: "colour"
Line 3: "colouur" - No match
Line 4: "color vs colour"
Match 1: "color"
Match 2: "colour"
All matches found: 4
Matches: ['color', 'colour', 'color', 'colour']