Predefined Character Classes

Predefined Character Classes

PatternDescriptionEquivalent
.Matches any character except newline
\dMatches any digit[0-9]
\DMatches any non-digit[^0-9]
\wMatches any word character[a-zA-Z0-9_]
\WMatches any non-word character[^a-zA-Z0-9_]
\sMatches any whitespace character[ \t\n\r\f\v]
\S



Matches any non-whitespace character[^ \t\n\r\f\v]

1. Literal Character a

Matches: The exact character ‘a’

Example 1:

python

import re
text = "apple banana cherry"
result = re.findall(r'a', text)
print(result)  # ['a', 'a', 'a', 'a']
# Matches all individual 'a' characters

Example 2:

python

text = "Python is amazing"
result = re.findall(r'is', text)
print(result)  # ['is']
# Matches the literal word "is"

2. Any Single Character .

Matches: Any single character except newline

Example 1:

python

text = "cat bat hat rat"
result = re.findall(r'.at', text)
print(result)  # ['cat', 'bat', 'hat', 'rat']
# Matches any character followed by "at"

Example 2:

python

text = "A1 B2 C3 D4"
result = re.findall(r'.\d', text)
print(result)  # ['A1', 'B2', 'C3', 'D4']
# Matches any character followed by a digit

3. Any Digit \d

Matches: Any digit (0-9)

Example 1:

python

text = "My phone number is 123-456-7890"
result = re.findall(r'\d', text)
print(result)  # ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
# Extracts all individual digits

Example 2:

python

text = "Version 2.5.1 released"
result = re.findall(r'\d\.\d\.\d', text)
print(result)  # ['2.5.1']
# Matches version number pattern

4. Any Non-Digit \D

Matches: Any character that is NOT a digit

Example 1:

python

text = "Room 101, Floor 2"
result = re.findall(r'\D', text)
print(result)  # ['R', 'o', 'o', 'm', ' ', ',', ' ', 'F', 'l', 'o', 'o', 'r', ' ']
# All non-digit characters

Example 2:

python

text = "Price: $19.99"
result = re.findall(r'\D+', text)
print(result)  # ['Price: $', '.']
# Non-digit sequences (with quantifier)

5. Word Character \w

Matches: Letters (a-z, A-Z), digits (0-9), and underscore (_)

Example 1:

python

text = "Hello_World123! Test@email.com"
result = re.findall(r'\w', text)
print(result)  # ['H', 'e', 'l', 'l', 'o', '_', 'W', 'o', 'r', 'l', 'd', '1', '2', '3', 'T', 'e', 's', 't', 'e', 'm', 'a', 'i', 'l', 'c', 'o', 'm']
# All word characters individually

Example 2:

python

text = "user_name123 is valid"
result = re.findall(r'\w+', text)
print(result)  # ['user_name123', 'is', 'valid']
# Complete words including underscores and numbers

6. Non-Word Character \W

Matches: Anything NOT a word character (punctuation, spaces, symbols)

Example 1:

python

text = "Hello, World! How are you?"
result = re.findall(r'\W', text)
print(result)  # [',', ' ', '!', ' ', ' ', ' ', '?']
# All non-word characters

Example 2:

python

text = "Email: test@example.com"
result = re.findall(r'\W+', text)
print(result)  # [': ', '@', '.']
# Sequences of non-word characters

7. Whitespace Character \s

Matches: Spaces, tabs, newlines, and other whitespace characters

Example 1:

python

text = "Hello\tWorld\nHow are you?"
result = re.findall(r'\s', text)
print(result)  # ['\t', '\n', ' ', ' ', ' ']
# All whitespace characters

Example 2:

python

text = "Python    is   great"
result = re.sub(r'\s+', ' ', text)
print(result)  # "Python is great"
# Replace multiple spaces with single space

8. Non-Whitespace Character \S

Matches: Any character that is NOT whitespace

Example 1:

python

text = "Hello World!\nPython is fun."
result = re.findall(r'\S', text)
print(result)  # ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd', '!', 'P', 'y', 't', 'h', 'o', 'n', 'i', 's', 'f', 'u', 'n', '.']
# All non-whitespace characters

Example 2:

python

text = "  Remove  extra   spaces  "
result = re.findall(r'\S+', text)
print(result)  # ['Remove', 'extra', 'spaces']
# Extract all words ignoring spaces

9. Start of String ^

Matches: The beginning of the string (or line with multiline flag)

Example 1:

python

text = "Hello World\nHello Python"
result = re.findall(r'^Hello', text)
print(result)  # ['Hello']
# Matches "Hello" only at start of string

Example 2:

python

text = "Hello World\nHello Python"
result = re.findall(r'^Hello', text, re.MULTILINE)
print(result)  # ['Hello', 'Hello']
# Matches "Hello" at start of each line

10. End of String $

Matches: The end of the string (or line with multiline flag)

Example 1:

python

text = "Hello World\nHello Python"
result = re.findall(r'Python$', text)
print(result)  # ['Python']
# Matches "Python" only at end of string

Example 2:

python

text = "First line\nSecond line\nThird line"
result = re.findall(r'line$', text, re.MULTILINE)
print(result)  # ['line', 'line', 'line']
# Matches "line" at end of each line

Combined Example Showing Multiple Patterns

python

text = "User: john_doe123, Email: john@example.com, Age: 25"

# Extract username (word characters)
username = re.findall(r'User: (\w+)', text)
print("Username:", username)  # ['john_doe123']

# Extract email (word chars with @ and .)
email = re.findall(r'Email: (\S+@\S+\.\S+)', text)
print("Email:", email)  # ['john@example.com']

# Extract age (digits at end of phrase)
age = re.findall(r'Age: (\d+)$', text)
print("Age:", age)  # ['25']

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *