What are character classes or character sets used in Python regular expression?

Character class

A "character class", or a "character set", is a set of characters put in square brackets. The regex engine matches only one out of several characters in the character class or character set. We place the characters we want to match between square brackets. If you want to match any vowel, we use the character set [aeiou].

A character class or set matches only a single character. The order of the characters inside a character class or set does not matter. The results are identical.

We use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9. Similarly for uppercase and lowercase letters we have the character class [A-Za-z]


The following code finds and prints all the vowels in the given string

import re
s = 'mother of all battles'
result = re.findall(r'[aeiou]', s)
print result


This gives the output

['o', 'e', 'o', 'a', 'a', 'e']