Menu
Coddy logo textTech

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 characters a, b, or c.
  • [15]: Matches the digit 1 or 5.
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 icon

Challenge

Easy

Write 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 here

All lessons in RegEx in Python