Python RegEx - Find numbers of Specific Length in String
Python Regex - Find Numbers of Specific Length in String
To find numbers of a specific length N
in a string, use the regular expression [0-9]+ to extract all digit sequences, then filter the results to match the desired length.
[0-9] matches a single digit, while [0-9]+ matches one or more consecutive digits. By applying a filter function, you can extract numbers of the specified length.
Examples
1. Find numbers of specific length in a string
In this example, we extract all numbers of length 3 from a given string.
Python Program
import re
# Input string
str = 'We four guys live at 2nd street of Malibeu 521. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
# Extract all numbers
all_numbers = re.findall('[0-9]+', str)
print('All Numbers:', all_numbers)
# Specify the desired length
N = 3
# Function to filter numbers of length N
def filter_number(n):
return len(n) == N
# Filter the list
filtered_numbers = list(filter(filter_number, all_numbers))
print('Numbers of length', N, ':', filtered_numbers)
Explanation:
re.findall('[0-9]+', str)
extracts all digit sequences from the input string.- The variable
all_numbers
stores the list of extracted numbers:['2', '521', '248', '88796451', '52']
. - The function
filter_number(n)
checks if the length of each numbern
equals the specified lengthN
. filter()
applies the functionfilter_number
to all elements inall_numbers
.- The result is a list of numbers that are exactly
N
digits long:['521', '248']
.
Output:
All Numbers: ['2', '521', '248', '88796451', '52']
Numbers of length 3 : ['521', '248']
2. Extract numbers of specific length directly using regex
You can also use a regex pattern to directly match numbers of the desired length. For example, to extract 4-digit numbers:
Python Program
import re
# Input string
str = 'The zip codes are 1234, 56789, and 4321.'
# Extract 4-digit numbers directly
four_digit_numbers = re.findall('\b[0-9]{4}\b', str)
print('4-digit Numbers:', four_digit_numbers)
Explanation:
- The regex
'\b[0-9]{4}\b'
matches exactly 4-digit numbers: [0-9]
: Matches a single digit.{4}
: Matches exactly 4 consecutive digits.\b
: Ensures that the match is a whole word (avoids partial matches).- The result is a list of 4-digit numbers:
['1234', '4321']
.
Output:
4-digit Numbers: ['1234', '4321']
3. Extract numbers of varying lengths
You can extract numbers within a range of lengths, such as numbers with 2 to 5 digits.
Python Program
import re
# Input string
str = 'Values: 45, 6789, 123456, 89.'
# Extract numbers with 2 to 5 digits
numbers_in_range = re.findall('\b[0-9]{2,5}\b', str)
print('Numbers with 2 to 5 digits:', numbers_in_range)
Explanation:
- The regex
'\b[0-9]{2,5}\b'
matches numbers with 2 to 5 digits: {2,5}
: Matches between 2 and 5 consecutive digits.\b
: Ensures the match is a whole word.- The result is a list of numbers in the specified range:
['45', '6789', '12345', '89']
.
Output:
Numbers with 2 to 5 digits: ['45', '6789', '12345', '89']
4. Extract numbers including leading zeros
To include numbers with leading zeros, use the same regex patterns. For example:
Python Program
import re
# Input string
str = 'Account numbers: 0001, 1234, 00567.'
# Extract numbers of specific length
specific_length_numbers = re.findall('\b[0-9]{4}\b', str)
print('4-digit Numbers:', specific_length_numbers)
Explanation:
- The regex
'\b[0-9]{4}\b'
includes numbers with leading zeros, as it matches exact lengths without ignoring leading characters. - The result includes
'0001'
and'1234'
.
Output:
4-digit Numbers: ['0001', '1234']
Summary
In this tutorial, we explored how to extract numbers of specific lengths using Python Regular Expressions. We covered both direct and filtered approaches, handling varying lengths, ranges, and numbers with leading zeros. Regex patterns can be tailored for various real-world scenarios to efficiently extract desired numbers from text.