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

  • Keyword-Only Arguments in Python and mixed

    Keyword-Only Arguments in Python Keyword-only arguments are function parameters that must be passed using their keyword names. They cannot be passed as positional arguments. Syntax Use the * symbol in the function definition to indicate that all parameters after it are keyword-only: python def function_name(param1, param2, *, keyword_only1, keyword_only2): # function body Simple Examples Example 1: Basic Keyword-Only Arguments…

  • Variable Length Keyword Arguments in Python

    Variable Length Keyword Arguments in Python Variable length keyword arguments allow a function to accept any number of keyword arguments. This is done using the **kwargs syntax. Syntax python def function_name(**kwargs): # function body # kwargs becomes a dictionary containing all keyword arguments Simple Examples Example 1: Basic **kwargs python def print_info(**kwargs): print(“Information received:”, kwargs) print(“Type of…

  • Decorators in Python

    Decorators in Python A decorator is a function that modifies the behavior of another function without permanently modifying it. Decorators are a powerful tool that use closure functions. Basic Concept A decorator: Simple Example python def simple_decorator(func): def wrapper(): print(“Something is happening before the function is called.”) func() print(“Something is happening after the function is…

  • file properties and methods

    1. file.closed – Is the file door shut? Think of a file like a door. file.closed tells you if the door is open or closed. python # Open the file (open the door) f = open(“test.txt”, “w”) f.write(“Hello!”) print(f.closed) # Output: False (door is open) # Close the file (close the door) f.close() print(f.closed) # Output: True (door is…

  • Iterators in Python

    Iterators in Python An iterator in Python is an object that is used to iterate over iterable objects like lists, tuples, dictionaries, and sets. An iterator can be thought of as a pointer to a container’s elements. To create an iterator, you use the iter() function. To get the next element from the iterator, you…

Leave a Reply

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