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

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

  •  List operators,List Traversals

    In Python, lists are ordered, mutable collections that support various operations. Here are the key list operators along with four basic examples: List Operators in Python 4 Basic Examples 1. Concatenation (+) Combines two lists into one. python list1 = [1, 2, 3] list2 = [4, 5, 6] combined = list1 + list2 print(combined) # Output: [1, 2, 3,…

  • re.findall()

    Python re.findall() Method Explained The re.findall() method returns all non-overlapping matches of a pattern in a string as a list of strings or tuples. Syntax python re.findall(pattern, string, flags=0) Key Characteristics: Example 1: Extracting All Numbers from Text python import retext = “I bought 5 apples for $3.50, 2 bananas for $1.25, and 10 oranges for $7.80.”result = re.findall(r”\d{3}”,…

  • Data hiding

    Data hiding in Python OOP is the concept of restricting access to the internal data of an object from outside the class. 🔐 It’s a way to prevent direct modification of data and protect the object’s integrity. This is typically achieved by using a naming convention that makes attributes “private” or “protected.” 🔒 How Data…

  • Generators in Python

    Generators in Python What is a Generator? A generator is a special type of iterator that allows you to iterate over a sequence of values without storing them all in memory at once. Generators generate values on-the-fly (lazy evaluation) using the yield keyword. Key Characteristics Basic Syntax python def generator_function(): yield value1 yield value2 yield value3 Simple Examples Example…

  • File Handling in Python

    File Handling in Python File handling is a crucial aspect of programming that allows you to read from and write to files. Python provides built-in functions and methods to work with files efficiently. Basic File Operations 1. Opening a File Use the open() function to open a file. It returns a file object. python # Syntax: open(filename,…

Leave a Reply

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