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

  • Tuples

    In Python, a tuple is an ordered, immutable (unchangeable) collection of elements. Tuples are similar to lists, but unlike lists, they cannot be modified after creation (no adding, removing, or changing elements). Key Features of Tuples: Syntax: Tuples are defined using parentheses () (or without any brackets in some cases). python my_tuple = (1, 2, 3, “hello”) or (without…

  • Basic Character Classes

    Basic Character Classes Pattern Description Example Matches [abc] Matches any single character in the brackets a, b, or c [^abc] Matches any single character NOT in the brackets d, 1, ! (not a, b, or c) [a-z] Matches any character in the range a to z a, b, c, …, z [A-Z] Matches any character in the range A to Z A, B, C, …, Z [0-9] Matches…

  • pop(), remove(), clear(), and del 

    pop(), remove(), clear(), and del with 5 examples each, including slicing where applicable: 1. pop([index]) Removes and returns the item at the given index. If no index is given, it removes the last item. Examples: 2. remove(x) Removes the first occurrence of the specified value x. Raises ValueError if not found. Examples: 3. clear() Removes all elements from the list, making it empty. Examples: 4. del Statement Deletes elements by index or slice (not a method, but a…

  • Functions as Parameters in Python

    Functions as Parameters in Python In Python, functions are first-class objects, which means they can be: Basic Concept When we pass a function as a parameter, we’re essentially allowing one function to use another function’s behavior. Simple Examples Example 1: Basic Function as Parameter python def greet(name): return f”Hello, {name}!” def farewell(name): return f”Goodbye, {name}!” def…

  • Python timedelta Explained

    Python timedelta Explained timedelta is a class in Python’s datetime module that represents a duration – the difference between two dates or times. It’s incredibly useful for date and time arithmetic. Importing timedelta python from datetime import timedelta, datetime, date Basic Syntax python timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) Examples 1. Basic timedelta Creation python from datetime…

  • non-capturing group, Named Groups,groupdict()

    To create a non-capturing group in Python’s re module, you use the syntax (?:…). This groups a part of a regular expression together without creating a backreference for that group. A capturing group (…) saves the matched text. You can then access this captured text using methods like group(1), group(2), etc. A non-capturing group (?:…)…

Leave a Reply

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