UNIT – 5
CHARACTER CLASSES
In Python, regex character classes are sets of
characters or ranges of characters enclosed by
square brackets [].
For example, [a-z] it means match any lowercase
letter from a to z.
Let’s see some of the most common character classes
used inside regular expression patterns
Character Class Description
[abc] Match the letter a or b or c
Match letter a or b or c followed by either p
[abc][pq]
or q.
[^abc] Match any letter except a, b, or c (negation)
Match any digit from 0 to 9. inclusive
[0-9]
(range)
Match any lowercase letters from a to z.
[a-z]
inclusive (range)
Match any UPPERCASE letters from A to Z.
[A-Z]
inclusive (range)
Match any lowercase or UPPERCASE letter.
[a-zA-Z]
inclusive (range)
Ranges: matches a letter between m and p
[m-p2-8]
and digits from 2 to 8, but not p2
[a-zA-Z0-9] Match any alphanumeric character
[abc] Match the letter a or b or c
EXAMPLE : OUTPUT :
import re 0 ...... a
matcher=re.finditer("[abc]","a7b@k9z") 2 ...... b
for match in matcher:
print(match.start(),"......",match.group())
[^abc] Match any letter except a, b, or c (negation)
EXAMPLE : OUTPUT :
import re 1 ...... 7
matcher=re.finditer("[^abc]","a7b@k9z") 3 ...... @
for match in matcher: 4 ...... k
print(match.start(),"......", match.group()) 5 ...... 9
6 ...... Z
Match any lowercase letters from a to z.
[a-z]
inclusive (range)
EXAMPLE : OUTPUT :
import re 0 ...... a
matcher=re.finditer("[a-z]","a7b@k9z") 2 ...... b
for match in matcher : 4 ...... k
print(match.start(),"......", match.group()) 6 ...... z
[a-zA-Z0-9] Match any alphanumeric character
EXAMPLE : OUTPUT :
import re 0 ...... a
matcher=re.finditer("[a-zA-Z0-9]","a7b@k9z") 1 ...... 7
for match in matcher: 2 ...... b
print(match.start(),"......",match.group()) 4 ...... k
5 ...... 9
6 ...... z
Match any digit from 0 to 9. inclusive
[0-9]
(range)
EXAMPLE 4 : OUTPUT :
import re 1 ...... 7
matcher=re.finditer("[0-9]","a7b@k9z") 5 ...... 9
for match in matcher:
print(match.start(),"......",match.group())
Ranges: matches a letter between m and p
[m-p2-8]
and digits from 2 to 8, but not p2
EXAMPLE 5 : OUTPUT :
import re 1 ...... 7
matcher=re.finditer("[m-p2-8]","a7b@k9z")
for match in matcher:
print(match.start(),"......", match.group())
PREDEFINED CHARACTER CLASSES IN PYTHON:
\s : Space character
\S : Any character except space character
\d : Any digit from 0 to 9
\D : Any character except digit
\w : Any word character [a-zA-Z0-9]
\W : Any character except word character (Special Characters)
. : Any character including special characters
\s : Space character
EXAMPLE : OUTPUT :
import re 3 ......
matcher=re.finditer("\s","a7b @k9z")
for match in matcher:
print(match.start(),"......",match.group())
\S : Any character except space character
EXAMPLE : OUTPUT :
import re 0 ...... a
matcher=re.finditer("\S","a7b @k9z") 1 ...... 7
for match in matcher: 2 ...... b
4 ...... @
print(match.start(),"......",match.group())
5 ...... k
6 ...... 9
7 ...... z
\d : Any digit from 0 to 9
EXAMPLE : OUTPUT :
import re 1 ...... 7
6 ...... 9
matcher=re.finditer("\d","a7b @k9z")
for match in matcher:
print(match.start(),"......",match.group())
\D : Any character except digit
EXAMPLE : OUTPUT :
import re 0 ...... a
matcher=re.finditer("\D","a7b @k9z") 2 ...... b
for match in matcher: 3 ......
4 ...... @
print(match.start(),"......",match.group())
5 ...... k
7 ...... z
\w : Any word character [a-zA-Z0-9]
EXAMPLE : OUTPUT :
import re 0 ...... a
1 ...... 7
matcher=re.finditer("\w","a7b @k9z")
2 ...... b
for match in matcher:
5 ...... k
print(match.start(),"......",match.group()) 6 ...... 9
7 ...... z
\W : Any character except word
character (Special Characters)
EXAMPLE : OUTPUT :
import re 3 ......
matcher=re.finditer("\W","a7b @k9z") 4 ...... @
for match in matcher:
print(match.start(),"......",match.group())
. : Any character including special characters
EXAMPLE : OUTPUT :
import re 0 ...... a
1 ...... 7
matcher=re.finditer(".","a7b @k9z")
2 ...... b
for match in matcher:
3 ......
print(match.start(),"......",match.group()) 4 ...... @
5 ...... k
6 ...... 9
7 ...... z