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
EasyWrite 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 hereAll lessons in RegEx in Python
1Introduction
Introduction3Basic Pattern Syntax
Basic MetacharactersLiteralsCharacter ClassesRangesCommon Predefined ClassesAdvanced Predefined Classes