What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Use re.search(r"d+", text) to find the first contiguous run of decimal digits anywhere in a Python string. Check whether it returned None before calling group(); convert the matched text with int() only if you need a numeric value.
Find the first digit run with re.search
re.search scans through a string and returns a match object for the first place the pattern matches. If there is no match, it returns None. The match’s group() method returns the matched text.
import re
text = "Order 27 ships after 4 days"
match = re.search(r"d+", text)
if match is None:
first_number = None
else:
first_number = match.group()
print(first_number) # 27
Here, first_number is the string "27". If you need an integer instead, convert it after checking for a match:
first_number = int(match.group()) if match is not None else None
Why use search instead of match?
re.match checks for a match at the beginning of the string. re.search scans forward, so it can find digits after words or punctuation. For example, with "Order 27", re.search(r"d+", text) finds "27", while re.match does not match the digits at the start.
#1 Best Overall
See the Python 3.14.8 regular-expression documentation for the behavior of search, match, and match results.
Choose which digits count
Unicode decimal digits
In a pattern applied to a Python str, d matches Unicode decimal digits, not only the ASCII characters 0 through 9. Use it when that broader decimal-digit behavior is appropriate.
Rank #2
ASCII digits only
To restrict the match to the characters 0–9, use an explicit character class:
match = re.search(r"[0-9]+", text)
You can also keep d and set the ASCII flag:
match = re.search(r"d+", text, flags=re.ASCII)
Decide what “number” means for your input
The pattern d+ returns a contiguous run of digits. It does not include a preceding sign, decimal point, or exponent as part of the match. For example, it finds the digit runs in text such as "-12.5e3"; it does not parse that whole expression as one number.
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 glitchesIf the input can contain signed integers or floating-point values, define that grammar first and use a pattern that matches it. The Python regular-expression reference includes examples for signed integers and floating-point numbers.
Get the match position or find every run
Get the location
Keep the match object if you need the character index or range as well as the text. Use start() for the starting index or span() for the start and end indices:
if match is not None:
print(match.group()) # matched text
print(match.start()) # starting index
print(match.span()) # (start, end)
Find all digit runs
Use re.findall to get a list of matching strings, or re.finditer to get match objects with positions:
runs = re.findall(r"d+", text)
matches = list(re.finditer(r"d+", text))
The CPython Regular Expression HOWTO explains these extraction options and match-object use.
Best Value
Why str.isdigit() is not a substring search
str.isdigit() answers whether a whole non-empty string consists only of characters classified as digits. It does not locate the first digit run inside a larger string. Its character set is also broader than the Unicode decimal digits matched by a regex d. See the Python built-in types documentation for its precise definition.
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.




