
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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]
Example
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
Output
This gives the output
['o', 'e', 'o', 'a', 'a', 'e']
- Related Articles
- What are repeating character classes used in Python regular expression?
- What are metacharacters inside character classes used in Python regular expression?
- What are negated character classes that are used in Python regular expressions?
- Explain character classes in Java regular expressions
- How to escape any special character in Python regular expression?
- How to match a nonwhitespace character in python using Regular Expression?
- How to match a single character in python using Regular Expression?
- Find the non-digit character with JavaScript Regular Expression
- How to match any one uppercase character in python using Regular Expression?\n\n
- How to match any non-digit character in Python using Regular Expression?\n\n
- What are character class operations in Python?
- With JavaScript Regular Expression find a non-whitespace character.\n\n
- Posix character classes Java regex
- What are regular expression repetition cases in Python?
- What Python regular expression can be used in place of string.replace?
