Predefined Character Classes

UpComing SoftWare Training Demos

πŸš€ *Gen AI EngineerΒ Telugu (Production Focused)*
πŸ—“οΈ *Date:* 30th Sept 2026, 07:00AM IST
πŸ“ *Register Now!:*
https://www.vlrt.in/gr
πŸ‘₯ *Join WA Community:*
https://www.vlrt.in/gw
πŸ’‘*Course Content:*
https://www.vlrt.in/ai
▢️ *Demo Videos:*
https://www.vlrt.in/gv

πŸš€ *Service now Admin/ Development (ITSM)FREE Demo in Telugu*
πŸ—“οΈ *Date:* 30th Sept 2026, 08:00AM IST
πŸ“ *Register Now!:*
https://www.vlrt.in/7r
πŸ‘₯ *Join WA Community:*
https://www.vlrt.in/7w
πŸ’‘*Course Content:*
https://www.vlrt.in/7c
▢️ *Demo Videos:*
https://www.vlrt.in/7v

πŸš€ *Vulnerability Management Training Demo*
πŸ—“οΈ *Date:* 30th Sept 2026, 09:00 AM IST
πŸ“*Register Now!:*
https://www.vlrt.in/vr
πŸ‘₯ *Join Community:*
https://www.vlrt.in/cw
πŸ’‘ *Course Content:*
https://www.vlrt.in/vc
▢️ *Demo Videos:*
https://www.vlrt.in/Vm

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

  • How to create Class

    πŸŸ₯ Rectangle Properties Properties are the nouns that describe a rectangle. They are the characteristics that define a specific rectangle’s dimensions and position. Examples: πŸ“ Rectangle Methods Methods are the verbs that describe what a rectangle can do or what can be done to it. They are the actions that allow you to calculate information…

  • TheΒ Fractions module

    The Fractions module in Python is a built-in module that provides support for rational number arithmetic. It allows you to work with fractions (like 1/2, 3/4, etc.) exactly, without the precision issues that can occur with floating-point numbers. What Problems Does It Solve? Problem with Floating-Point Numbers: python # Floating-point precision issue print(0.1 + 0.2) # Output:…

  • reΒ module

    The re module is Python’s built-in module for regular expressions (regex). It provides functions and methods to work with strings using pattern matching, allowing you to search, extract, replace, and split text based on complex patterns. Key Functions in the re Module 1. Searching and Matching python import re text = “The quick brown fox jumps over the lazy dog” # re.search()…

  • Why Python is So Popular: Key Reasons Behind Its Global Fame

    Python’s fame and widespread adoption across various sectors can be attributed to its unique combination of simplicity, versatility, and a robust ecosystem. Here are the key reasons why Python is so popular and widely used in different industries: 1. Easy to Learn and Use 2. Versatility Python supports multiple programming paradigms, including: This versatility allows…

  • TheΒ print()Β Function in Python

    The print() Function in Python: Complete Guide The print() function is Python’s built-in function for outputting data to the standard output (usually the console). Let’s explore all its arguments and capabilities in detail. Basic Syntax python print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False) Arguments Explained 1. *objects (Positional Arguments) The values to print. You can pass multiple items separated by commas. Examples:…

  • 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…

Leave a Reply

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