Nearby lessons
124 of 159Python - Regex Character Classes
- Understand what a Character Class is and how it is written inside square brackets
- Apply each of the eight character classes - [abc], [^abc], [a-z], [A-Z], [a-zA-Z], [0-9], [a-zA-Z0-9], and [^a-zA-Z0-9] - to the target string a7b@k9z
- Learn that a caret ^ at the start of a Character Class means NOT or EXCEPT
- Learn that the hyphen - is used to represent a range of characters
- Read the output of re.finditer() using match.start() and match.group()
Introduction to Character Classes
In Regular Expressions, Character Classes are used to search for a particular group or category of characters.
A Character Class is generally written inside square brackets:
[ ]
For example:
[abc]
This pattern searches for:
a
or
b
or
c
Simple Definition:
A Character Class represents a set or range of characters that can be matched at a particular position.
A Character Class normally matches one character at a time.
Character Classes Covered
The following Character Classes are covered in this section:
| Character Class | Meaning |
|---|---|
[abc] |
Either a or b or c |
[^abc] |
Except a, b and c |
[a-z] |
Any lowercase alphabet symbol |
[A-Z] |
Any uppercase alphabet symbol |
[a-zA-Z] |
Any alphabet symbol |
[0-9] |
Any digit from 0 to 9 |
[a-zA-Z0-9] |
Any alphanumeric character |
[^a-zA-Z0-9] |
Any character except an alphanumeric character |
1. [abc] - Either a, b, or c
The Character Class:
[abc]
means:
Either
aorborc.
It matches exactly one character from the specified set.
[abc] │ ├── a ✓ ├── b ✓ ├── c ✓ ├── d ✗ ├── 7 ✗ └── @ ✗
For the target string:
a7b@k9z
the matching characters are:
a
b
There is no c in the target string.
Target string:
Character : a 7 b @ k 9 z
Index : 0 1 2 3 4 5 6
The pattern checks every character against:
a OR b OR c
| Index | Character | Match? |
|---|---|---|
| 0 | a | Yes |
| 1 | 7 | No |
| 2 | b | Yes |
| 3 | @ | No |
| 4 | k | No |
| 5 | 9 | No |
| 6 | z | No |
Therefore, matches are found at indexes 0 and 2.
Important Note: [abc] does not mean the complete string abc. It means one character that is either a, b, or c.
2. [^abc] - Except a, b, and c
The Character Class:
[^abc]
means:
Any character except
a,b, andc.
The caret ^ has a special meaning when it appears at the beginning inside a Character Class.
[^abc] │ ▼ NOT a, b or c
Examples:
a → ✗
b → ✗
c → ✗
d → ✓
7 → ✓
@ → ✓
The target string is:
a7b@k9z
The pattern excludes:
a
b
c
Therefore:
a → Not Matched
7 → Matched
b → Not Matched
@ → Matched
k → Matched
9 → Matched
z → Matched
Hence the matches occur at indexes:
1, 3, 4, 5, 6
3. [a-z] - Any Lowercase Alphabet
The Character Class:
[a-z]
means:
Any lowercase alphabet symbol from
atoz.
[a-z] │ ▼ a b c d ... x y z
It does not match:
- Uppercase letters
- Digits
- Special characters
The lowercase alphabet characters in:
a7b@k9z
are:
a
b
k
z
Their indexes are:
0
2
4
6
Digits 7 and 9 and the special character @ do not match.
4. [A-Z] - Any Uppercase Alphabet
The Character Class:
[A-Z]
means:
Any uppercase alphabet symbol from
AtoZ.
[A-Z] │ ▼ A B C D ... X Y Z
Our target string is:
a7b@k9z
The target contains:
a 7 b @ k 9 z
There are no uppercase letters.
Hence:
[A-Z] → No Match
Because the loop receives no Match Objects, nothing is printed.
5. [a-zA-Z] - Any Alphabet Symbol
The Character Class:
[a-zA-Z]
means:
Any alphabet symbol, either lowercase or uppercase.
It combines:
[a-z]
+
[A-Z]
Therefore:
a to z → Match
A to Z → Match
Digits and special characters do not match.
The alphabet symbols in the target are:
a
b
k
z
All of them are lowercase, but lowercase letters are included in:
[a-zA-Z]
Therefore, they match at indexes:
0, 2, 4, 6
6. [0-9] - Any Digit
The Character Class:
[0-9]
means:
Any digit from
0to9.
[0-9] │ ▼ 0 1 2 3 4 5 6 7 8 9
It matches one digit at a time.
The digits in:
a7b@k9z
are:
7
9
Their positions are:
7 → index 1
9 → index 5
Therefore:
1 ... 7
5 ... 9
7. [a-zA-Z0-9] - Any Alphanumeric Character
The Character Class:
[a-zA-Z0-9]
means:
Any alphanumeric character.
Alphanumeric means:
Alphabet
+
Digits
Therefore, this pattern matches:
- Lowercase letters:
a-z - Uppercase letters:
A-Z - Digits:
0-9
It does not match special characters.
The target is:
a7b@k9z
Check each character:
| Character | Type | Match? |
|---|---|---|
| a | Alphabet | Yes |
| 7 | Digit | Yes |
| b | Alphabet | Yes |
| @ | Special Character | No |
| k | Alphabet | Yes |
| 9 | Digit | Yes |
| z | Alphabet | Yes |
Only @ is not alphanumeric.
8. [^a-zA-Z0-9] - Except Alphanumeric Characters
The Character Class:
[^a-zA-Z0-9]
means:
Any character except an alphanumeric character.
In simple words, it searches for characters that are not:
a-zA-Z0-9
Therefore, it is useful for finding special characters.
[^a-zA-Z0-9]
│
▼
Not Alphabet
AND
Not Digit
│
▼
Special Character
The target string contains:
a 7 b @ k 9 z
All characters except @ are alphanumeric.
The @ symbol is a special character.
It occurs at index:
3
Therefore:
3 ... @
Complete Demo Program
In the demo program, we can change only the Regular Expression while keeping the target string the same.
matcher = re.finditer("[abc]", "a7b@k9z")
Replace [abc] with:
[^abc]
[a-z]
[A-Z]
[a-zA-Z]
[0-9]
[a-zA-Z0-9]
[^a-zA-Z0-9]
This allows us to understand the behaviour of every Character Class using the same target string.
All Outputs
Pattern: [abc]
0 ... a
2 ... b
Pattern: [^abc]
1 ... 7
3 ... @
4 ... k
5 ... 9
6 ... z
Pattern: [a-z]
0 ... a
2 ... b
4 ... k
6 ... z
Pattern: [A-Z]
No Output
Pattern: [a-zA-Z]
0 ... a
2 ... b
4 ... k
6 ... z
Pattern: [0-9]
1 ... 7
5 ... 9
Pattern: [a-zA-Z0-9]
0 ... a
1 ... 7
2 ... b
4 ... k
5 ... 9
6 ... z
Pattern: [^a-zA-Z0-9]
3 ... @
Complete Character-by-Character Analysis
Target string:
a7b@k9z
| Index | Character | Type |
|---|---|---|
| 0 | a | Lowercase Alphabet |
| 1 | 7 | Digit |
| 2 | b | Lowercase Alphabet |
| 3 | @ | Special Character |
| 4 | k | Lowercase Alphabet |
| 5 | 9 | Digit |
| 6 | z | Lowercase Alphabet |
This single target string contains:
Lowercase Alphabets → a, b, k, z
Digits → 7, 9
Special Character → @
Uppercase Alphabets → None
That is why it is useful for demonstrating all the Character Classes.
Complete Comparison Table
All eight Character Classes compared on the same target string a7b@k9z:
| Pattern | Meaning | Matches in a7b@k9z |
|---|---|---|
[abc] |
a or b or c | a, b |
[^abc] |
Except a, b and c | 7, @, k, 9, z |
[a-z] |
Lowercase alphabet | a, b, k, z |
[A-Z] |
Uppercase alphabet | No Match |
[a-zA-Z] |
Any alphabet | a, b, k, z |
[0-9] |
Any digit | 7, 9 |
[a-zA-Z0-9] |
Any alphanumeric character | a, 7, b, k, 9, z |
[^a-zA-Z0-9] |
Except alphanumeric characters | @ |
Positive and Negative Character Classes
Character Classes can be understood in two broad groups.
Positive Character Classes
These specify the characters that should match.
[abc]
[a-z]
[A-Z]
[a-zA-Z]
[0-9]
[a-zA-Z0-9]
For example:
[a-z]
means:
Match lowercase alphabet characters.
Negative Character Classes
A caret ^ at the beginning inside the square brackets means exclusion.
[^abc]
[^a-zA-Z0-9]
For example:
[^abc]
means:
Match anything except a, b and c.
Important Meaning of ^ Inside a Character Class
When ^ appears immediately after the opening square bracket:
[^...]
it means:
NOT
or
EXCEPT
Example:
[abc]
means:
a OR b OR c
But:
[^abc]
means:
anything EXCEPT a, b and c
This difference is very important.
Understanding the Range Symbol (-)
The hyphen - is used to represent a range inside Character Classes.
Examples:
[a-z]
[A-Z]
[0-9]
They represent:
[a-z] → a through z
[A-Z] → A through Z
[0-9] → 0 through 9
Multiple ranges can also be combined:
[a-zA-Z0-9]
which represents lowercase letters, uppercase letters, and digits.
Execution Flow of the Demo Program
The general execution flow of the demo program:
Program Starts
│
▼
Import re Module
│
▼
Define Character Class
Example: [abc]
│
▼
Target String
"a7b@k9z"
│
▼
Call re.finditer()
│
▼
Check Character at Index 0
│
▼
Does Character Match Pattern?
│
┌──┴───┐
│ │
Yes No
│ │
▼ ▼
Create Move to
Match Next Character
Object
│
▼
Print
start()
group()
│
▼
Check Next Character
│
▼
Repeat Until End
of Target String
│
▼
No More Characters
│
▼
Program Ends
Using the same target:
a7b@k9z
the Regular Expression engine produces:
[abc] │ ▼ a, b [^abc] │ ▼ 7, @, k, 9, z [a-z] │ ▼ a, b, k, z [A-Z] │ ▼ No Match [a-zA-Z] │ ▼ a, b, k, z [0-9] │ ▼ 7, 9 [a-zA-Z0-9] │ ▼ a, 7, b, k, 9, z [^a-zA-Z0-9] │ ▼ @
- A Character Class represents a set or range of characters that can be matched at a particular position
- [abc] matches either a, b, or c, while [^abc] matches any character except a, b, and c
- [a-z], [A-Z], and [0-9] are ranges for lowercase letters, uppercase letters, and digits
- [a-zA-Z0-9] matches alphanumeric characters, while [^a-zA-Z0-9] matches non-alphanumeric (special) characters
- re.finditer() returns matches from left to right, with match.start() giving the index and match.group() giving the matched character