Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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, so it 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]"

Each pattern is designed to get 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, such as -

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 module
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(res)  

Output

This will create the following outcome -

['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. This function scans the text and returns a list of all matching characters.

# Import re module
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(res) 

Output

This will generate the following result -

['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 module
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(res) 

Output

This will produce the following result -

['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). So the output will return the letters in lower and uppercase with numbers, but it will not include any special characters.

# Import re module
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(res) 

Output

This will lead to the following outcome -

['D', 'a', 't', 'a', '1', '2', '3', 'S', 'c', 'i', 'e', 'n', 'c', 'e']
Updated on: 2025-05-26T18:00:53+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements