Menu
CoddyTech

Advanced Predefined Classes

Lesson 11 of 28 in Coddy's RegEx in Python course.

Here are some additional predefined character classes that are the complements of the common ones:

  • <strong>\D</strong>: Matches any non-digit character (equivalent to [^0-9]).
  • <strong>\W</strong>: Matches any non-word character (equivalent to [^a-zA-Z0-9_]).
  • <strong>\S</strong>: Matches any non-whitespace character (equivalent to [^ \t\n\r\f\v]).
import re

text = "Hello, world! Welcome to regex 101."
pattern = r"\W"

matches = re.findall(pattern, text)

print(matches)  # Output: [',', ' ', '!', ' ', ' ', ' ', ' ', '.']

In the above example, the pattern \W matches any non-word character in the string text.

challenge icon

Challenge

Easy

Write a function named find_non_digit_characters that takes a text string as input and returns a list of all non-digit characters found in the text.

Try it yourself

import re

def find_non_digit_characters(text):
    # Write code here

All lessons in RegEx in Python

Practice on your own: Online Python compiler

Test a pattern on your own text: Regex Tester