To extract runs of decimal digits from text, use Python’s regular-expression module: re.findall(r'd+', text). It returns matches as strings, so choose a conversion such as int() only after deciding what formats your input can contain.
Extract digit sequences with re.findall()
For ordinary runs of digits, d+ means “one or more consecutive decimal digits.” With no capturing groups, re.findall() returns each full match in a list:
As an Amazon Associate I earn from qualifying purchases.
import re
text = "Order 17 contains 3 items"
numbers = re.findall(r"d+", text)
print(numbers) # ['17', '3']
The result contains strings, not numeric values. Use a raw string such as r"d+" for the pattern so Python handles the backslash as part of the regular expression. See Python’s regular-expression documentation.
Free tools Windows power users keep installed
One-click scans. No signup required.
Choose a pattern that matches your number format
A regex finds the character sequences its pattern describes; it does not infer every possible way a number might be written. Decide which formats are valid in your input before extracting or converting matches.
#1 Best Overall
| Need | Example pattern or method | What it handles |
|---|---|---|
| Unsigned digit runs | r'd+' |
One or more consecutive Unicode decimal digits in a Python str pattern. |
| ASCII digits only | r'[0-9]+' |
One or more characters from ASCII 0 through 9. |
| Optional sign and decimal fraction | r'[+-]?d+(?:.d+)?' |
An optional plus or minus, digits, and an optional period followed by digits. It does not include exponents, grouped digits, or forms such as .5. |
| Match locations as well as text | re.finditer(pattern, text) |
Each match’s value and its start and end positions. |
The signed-decimal pattern is only an example grammar. For example, it will match the digits in 1e3 separately rather than treating the text as an exponent-form number. Extend or replace the pattern to fit the notation your data actually uses; locale-specific separators and digit grouping also need explicit handling.
Get match positions with re.finditer()
When you need to know where each match occurs—for example, to highlight it in the original text—iterate over match objects rather than collecting strings alone:
Rank #2
import re
text = "Order 17 contains 3 items"
for match in re.finditer(r"d+", text):
print(match.group(), match.start(), match.end())
group() returns the matched text; start() and end() give its character positions in the input string. Use findall() when the matched strings are all you need.
Understand what findall() returns with groups
With no capturing group, findall() returns full matches. If the pattern has one capturing group, it returns that group’s text; with multiple capturing groups, it returns tuples. If parentheses are needed to group part of a pattern without changing the result shape, use a non-capturing group such as (?:.d+)?.
Decide how Unicode digits should behave
In a Unicode str regex, d matches Unicode decimal digits, not only ASCII 0–9. If your input must use ASCII digits, write [0-9] or use the re.ASCII flag.
Python’s string tests describe different character classes: isdecimal() recognizes decimal characters, isdigit() also accepts certain additional characters such as superscript digits, and isnumeric() is broader still. A character accepted by isdigit() is not necessarily suitable for an ordinary base-10 numeral. These methods test a string as a whole; they do not scan prose for embedded values. See the built-in types documentation.
Convert extracted strings only when appropriate
Use int(value) for matched strings that follow the integer syntax it accepts. Use float(value) only when the text follows Python’s accepted float syntax. A regex can deliberately allow text that a chosen conversion function rejects, so handle conversion errors when that is possible.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesvalues = re.findall(r"d+", "Order 17 contains 3 items")
integers = [int(value) for value in values]
# [17, 3]
For data with signs, decimals, exponents, or separators, make the extraction pattern and conversion step agree on the same grammar. If numbers are tokens within a larger structured language rather than loose text, Python’s regex documentation shows a named NUMBER pattern used as part of tokenization.
Best Value
Check the pattern against your input
- Choose whether you want digit runs or complete signed and decimal numbers.
- Test representative cases, including punctuation, adjacent text, and input with no match.
- Decide whether Unicode decimal digits or ASCII digits are valid.
- Use
finditer()if match positions matter; otherwise usefindall(). - Confirm that the conversion function accepts every matched form, and handle failures where necessary.
The examples use standard-library interfaces documented for Python 3.14.8 (re) and Python 3.14.7 (built-in types). Check the documentation for the Python version installed in your environment.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




