Character Classes
Lesson 8 of 28 in Coddy's RegEx in Python course.
Character classes allow you to define a set of characters that you want to match. They are created using square brackets [], and they match any one character within the brackets.
Here are some basic examples of character classes:
[abc]: Matches any one of the charactersa,b, orc.[15]: Matches the digit1or5.
import re
text = "bat, bet, bit, but, bot"
pattern = "b[aeiou]t"
matches = re.findall(pattern, text)
print(matches) # Output: ['bat', 'bet', 'bit', 'but', 'bot']In the above example, the pattern b[aeiou]t matches any three-letter word that starts with "b" and ends with "t", with any vowel in between.
Challenge
EasyWrite a function named find_vowel_words that takes a text string as input and returns a list of words that start with "c", followed by any vowel (a, e, i, o, u), and end with "t".
Try it yourself
import re
def find_vowel_words(text):
# Write code hereAll lessons in RegEx in Python
1Introduction
Introduction3Basic Pattern Syntax
Basic MetacharactersLiteralsCharacter ClassesRangesCommon Predefined ClassesAdvanced Predefined Classes