Python Regex - Get List of all Numbers in String
Python Regex - Get List of All Numbers from String
To extract all the numbers in a string as a list, use the regular expression '[0-9]+' with the re.findall() method. Here's how the regex works:
- [0-9]: Matches a single digit from 0 to 9.
- [0-9]+: Matches one or more continuous digits (a number).
re.findall() returns a list of all matches found in the string. If no matches are found, it returns an empty list.
numbers = re.findall('[0-9]+', str)Here, str is the input string from which numbers will be extracted.
Examples
1. Extract all numbers from a string
In this example, we extract numbers from the string 'We live at 9-162 Malibeu. My phone number is 666688888.'.
Python Program
import re
str = 'We live at 9-162 Malibeu. My phone number is 666688888.'
# Extract numbers using regex
numbers = re.findall('[0-9]+', str)
print(numbers)Explanation:
- The string
strcontains the text from which numbers need to be extracted. - The regex pattern
'[0-9]+'matches all sequences of one or more digits in the string. re.findall()finds all matches and returns them as a list of strings.- The extracted numbers are printed as
['9', '162', '666688888'].
Output
['9', '162', '666688888']2. Extract numbers from a string with varying contexts
We extract numbers from the string 'We four guys live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'.
Python Program
import re
str = 'We four guys live at 2nd street of Malibeu. I had a cash of $248 in my pocket. I got a ticket with serial number 88796451-52.'
# Extract numbers using regex
numbers = re.findall('[0-9]+', str)
print(numbers)Explanation:
- The regex pattern
'[0-9]+'matches any sequence of digits. - It extracts
'2'(from '2nd'),'248','88796451', and'52'as separate numbers. re.findall()returns the list['2', '248', '88796451', '52'].
Output
['2', '248', '88796451', '52']3. Extract floating-point numbers
We extract both integers and floating-point numbers from the string 'The price is 123.45 and the discount is 20.5%.'.
Python Program
import re
str = 'The price is 123.45 and the discount is 20.5%.'
# Extract floating-point numbers using regex
numbers = re.findall('[0-9]*\.[0-9]+|[0-9]+', str)
print(numbers)Explanation:
- The regex
'[0-9]*\.[0-9]+|[0-9]+'matches floating-point numbers and integers. [0-9]*\.[0-9]+: Matches numbers with a decimal point (e.g., '123.45', '20.5').[0-9]+: Matches integers.- All matches are returned as
['123.45', '20.5'].
Output
['123.45', '20.5']4. Extract numbers with negative values
We extract numbers, including negative values, from the string 'The temperature ranges from -10 to 25 degrees.'.
Python Program
import re
str = 'The temperature ranges from -10 to 25 degrees.'
# Extract numbers including negatives using regex
numbers = re.findall('-?[0-9]+', str)
print(numbers)Explanation:
- The regex
'-?[0-9]+'matches numbers with an optional leading minus sign. - The extracted numbers include
'-10'and'25'. - All matches are returned as
['-10', '25'].
Output
['-10', '25']5. Extract numbers with units (advanced)
We extract numbers along with their units from the string 'The package weighs 2kg and the distance is 10m.'.
Python Program
import re
str = 'The package weighs 2kg and the distance is 10m.'
# Extract numbers with units using regex
numbers_with_units = re.findall('[0-9]+[a-zA-Z]+', str)
print(numbers_with_units)Explanation:
- The regex
'[0-9]+[a-zA-Z]+'matches numbers followed by letters (e.g., '2kg', '10m'). - All matches are returned as
['2kg', '10m'].
Output
['2kg', '10m']Summary
In this tutorial, we learned how to extract all numbers from a string using Python Regular Expressions. We explored various use cases, such as extracting integers, floating-point numbers, negative numbers, and numbers with units. Using the re.findall() method with appropriate regex patterns makes it easy to handle these scenarios in Python.