Skip to main content

Character Classes and Quantifiers

Predefined Character Classes

0:00
LearnStep 1/3

Digit and Word Character Classes

Digit and Word Character Classes

Regular expressions provide shorthand codes (often called metacharacters) to represent common sets of characters. Instead of writing verbose ranges like [0-9] or [a-zA-Z0-9_], you can use these predefined character classes to make your patterns more readable and concise.

Matching Digits: \d and \D

The pattern \d matches any single digit character (0-9). It is equivalent to the character set [0-9].

Conversely, \D (capital D) matches any character that is not a digit.

python

Matching Word Characters: \w and \W

The pattern \w matches any "word character". In Python's default mode (ASCII), this includes:

  • Uppercase and lowercase letters (a-z, A-Z)
  • Digits (0-9)
  • The underscore character (_)

Think of \w as characters typically allowed in variable names. The uppercase \W matches any character that is not a word character (e.g., whitespace, symbols like @, #, $, .).

python