How to use range in Python regular expression?

In this article, you will find out how to use the range in Python regular expressions. With the help of Python's regular expressions, you can search, match, and manipulate text efficiently. One useful feature in regex is ranges, which helps us define sets of characters within square brackets "[ ]".

Range in Regex

In Python, a range is used when you want to match characters within a specific group. It is defined inside brackets "[ ]" with the help of a hyphen ?. For example:

# Matches any digit (0 to 9)
pat1 = r"[0-9]"     

# Matches lowercase letters (a to z)
pat2 = r"[a-z]"     

# Matches uppercase letters (A to Z)
pat3 = r"[A-Z]"

# Matches both uppercase and lowercase letters
pat4 = r"[a-zA-Z]"

print("Digit pattern:", pat1)
print("Lowercase pattern:", pat2)
print("Uppercase pattern:", pat3)
print("Mixed case pattern:", pat4)
Digit pattern: [0-9]
Lowercase pattern: [a-z]
Uppercase pattern: [A-Z]
Mixed case pattern: [a-zA-Z]

Each pattern is designed to match specific characters as per their defined range.

Regex Range Examples in Python

Let us see different examples to show the usage of range in Python regular expression:

Example: Match Numbers Using Range

The following example will extract numbers from a given text. We have used a regex pattern that matches any digit from 0 to 9. To find all occurrences of numbers, we have used the re.findall() function. This function scans the text and returns a list of all matching digits ?

import re

# Define text here
txt = "The cost of mobile is Rs.15000"

# Define range pattern here to match any digit (0-9)
pat = r"[0-9]"  

# Use findall() method
res = re.findall(pat, txt)

# Print the result
print("Individual digits found:", res)  
Individual digits found: ['1', '5', '0', '0', '0']

Example: Match Lowercase Letters

The below example will extract lowercase letters from a given text. We have used a regex pattern that matches any letter from a to z. To find all occurrences of lowercase letters, we have used the re.findall() function ?

import re

# Define text
txt = "Python is Amazing!"

# Define range pattern to match lowercase letters (a-z)
pat = r"[a-z]"  

# Use findall() method to find matches
res = re.findall(pat, txt)

# Print the result
print("Lowercase letters found:", res) 
Lowercase letters found: ['y', 't', 'h', 'o', 'n', 'i', 's', 'm', 'a', 'z', 'i', 'n', 'g']

Example: Match Upper and Lowercase Letters

This program will extract both uppercase and lowercase letters from a given text. Here, the regex pattern we have used matches any letter from A to Z or a to z ?

import re

# Define text
txt = "Hello World 2025!"

# Define range pattern to match both uppercase and lowercase letters
pat = r"[a-zA-Z]"  

# Use findall() method to find matches
res = re.findall(pat, txt)

# Print the result
print("All letters found:", res) 
All letters found: ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']

Example: Extract Alphanumeric Characters

Now we will extract letters and digits from a given text. We have used a regex pattern that matches uppercase letters (A-Z), lowercase letters (a-z), and digits (0-9). The output will return letters in both cases with numbers, but will exclude special characters ?

import re

# Define text
txt = "Data123 @Science!"

# Define range pattern to match letters and digits
pat = r"[a-zA-Z0-9]"  

# Use findall() method to find matches
res = re.findall(pat, txt)

# Print the result
print("Alphanumeric characters found:", res) 
Alphanumeric characters found: ['D', 'a', 't', 'a', '1', '2', '3', 'S', 'c', 'i', 'e', 'n', 'c', 'e']

Advanced Range Patterns

You can also use more specific ranges and combine multiple ranges in a single pattern ?

import re

txt = "Phone: 123-456-7890, Code: ABC123"

# Match specific digit ranges
specific_digits = re.findall(r"[1-5]", txt)
print("Digits 1-5:", specific_digits)

# Match vowels only
vowels = re.findall(r"[aeiouAEIOU]", txt)
print("Vowels found:", vowels)

# Match letters and hyphens
letters_hyphens = re.findall(r"[a-zA-Z-]", txt)
print("Letters and hyphens:", letters_hyphens)
Digits 1-5: ['1', '2', '3', '4', '5']
Vowels found: ['o', 'e', 'A']
Letters and hyphens: ['P', 'h', 'o', 'n', 'e', 'C', 'o', 'd', 'e', 'A', 'B', 'C']

Conclusion

Ranges in Python regex allow you to match specific character sets efficiently using square brackets and hyphens. Use patterns like [0-9] for digits, [a-z] for lowercase letters, and combine ranges like [a-zA-Z0-9] for complex matching requirements.

Updated on: 2026-03-24T19:13:10+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements