A character class matches characters selected by a property or membership condition. Use shorthand classes such as \d for digits, \s for whitespace, and \w for word characters, or define a set with brackets such as [lmn]. section 2.4.3
Build bracketed classes from individual characters, ranges, or complements:
- [abcxyz] matches any listed character, while [a-z] and [A-Z] match lowercase and uppercase ranges. section 2.4.3
- Combine ranges, as in [a-dw-z], to match characters from either range. section 2.4.3
- Use complementary shorthand classes such as \D, \S, and \W for nondigits, nonwhitespace, and nonword characters; use [^a] for any character except a. section 2.4.3
Inspecting character classes
For test_text = "#1$2m_ M\t", re.findall("\\d", test_text) returns ['1', '2'], while re.findall("[lmn]", test_text) returns ['m']. The bracketed class selects only characters in its set. section 2.4.3
\d any decimal digit \D any character that is not a decimal digit \s any whitespace, including space, \t, \n, \r, \f, \v \S any character that isn't a whitespace \w any word character, means alphanumeric plus underscores \W any character that is not a word character . any character except a newline [] a set of defined characters
Compare the common shorthand classes, the dot wildcard, and bracket notation in one quick reference. section 2.4.3








