Character Classes

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

Overview

Match specific sets of characters using character classes and ranges

01 Simple Class

Square brackets [] define a character class that matches any one character inside the brackets.

Pattern breakdown:

  • [aeiou] - matches any single vowel

In "apple", this matches 'a', then 'e'. In "rhythm", there are no vowels so no matches occur.

Source: Regular-Expressions.info - Character Classes

Pattern: [aeiou]
Test Input
apple
banana
cherry
rhythm
Match Results
Line 1: "apple"
Match 1: "a"
Match 2: "e"
Line 2: "banana"
Match 1: "a"
Match 2: "a"
Match 3: "a"
Line 3: "cherry"
Match 1: "e"
Line 4: "rhythm" - No match
All matches found: 6
Matches: ['a', 'e', 'a', 'a', 'a', 'e']

02 Ranges

Character ranges use a hyphen to specify a range of characters. [0-9] matches any digit.

Pattern breakdown:

  • [0-9]+ - one or more digits
  • Matches entire number sequences

This finds "3", "2", "2024", "555", and "1234" in the test input.

Source: Python re Documentation

Pattern: [0-9]+
Test Input
I have 3 cats and 2 dogs.
The year 2024 was great.
No numbers here!
Call me at 555-1234.
Match Results
Line 1: "I have 3 cats and 2 dogs."
Match 1: "3"
Match 2: "2"
Line 2: "The year 2024 was great."
Match 1: "2024"
Line 3: "No numbers here!" - No match
Line 4: "Call me at 555-1234."
Match 1: "555"
Match 2: "1234"
All matches found: 5
Matches: ['3', '2', '2024', '555', '1234']

03 Negated Class

A caret ^ at the start of a character class negates it, matching any character NOT in the class.

Pattern breakdown:

  • [^0-9]+ - one or more characters that are NOT digits

This matches "abc", "def", "test", and "data" - all the non-numeric parts.

Source: Regular-Expressions.info - Negated Character Classes

Pattern: [^0-9]+
Test Input
abc123def
456
test789data
Match Results
Line 1: "abc123def"
Match 1: "abc"
Match 2: "def"
Line 2: "456" - No match
Line 3: "test789data"
Match 1: "test"
Match 2: "data"
All matches found: 4
Matches: ['abc', 'def\n', '\ntest', 'data']

04 Shorthand Classes

Shorthand character classes provide convenient shortcuts for common patterns.

Common shorthands:

  • \w - word character [a-zA-Z0-9_]
  • \d - digit [0-9]
  • \s - whitespace [ \t\n\r\f\v]
  • \W, \D, \S - negated versions

The pattern \w+ matches sequences of word characters: "Hello", "World", "test_variable_123", "price", "49", "99".

Source: Python re Syntax Documentation

Pattern: \w+
Test Input
Hello World!
test_variable_123
price: $49.99
Match Results
Line 1: "Hello World!"
Match 1: "Hello"
Match 2: "World"
Line 2: "test_variable_123"
Match 1: "test_variable_123"
Line 3: "price: $49.99"
Match 1: "price"
Match 2: "49"
Match 3: "99"
All matches found: 6
Matches: ['Hello', 'World', 'test_variable_123', 'price', '49', '99']