Just use re.search which stops matching once it finds a match.

re.search(r'\d+', headline).group()

or

You must remove the forward slashes present in your regex.

re.findall(r'^\D*(\d+)', headline)
Answer from Avinash Raj on Stack Overflow
๐ŸŒ
Python Guides
pythonguides.com โ€บ find-first-number-in-string-in-python
Extract the First Number from a String - Python Guides
January 12, 2026 - To find the first number in a string using Python, you can utilize the re module with a regular expression. First, import the re module and define a pattern to match one or more digits using r'\d+'. Then, use re.search(pattern, your_string) to search for the pattern in the string.
Discussions

python - Extract the first number from a string number range - Stack Overflow
I have a dataset with price column as type of string, and some of the values in the form of range (15000-20000). I want to extract the first number and convert the entire column to integers. I trie... More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 30, 2022
How to extract the first numbers in a string - Python - Stack Overflow
EDIT: This has numbers without ... the first numbers, not all of the numbers. ... At the risk of sounding snarky, I'd say you should try writing a function in python to take care of this. For starters, you can iterate over a string and to test whether each character is a number or not.. for char in myString: ... ... >>> from itertools ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to extract numbers from a string in Python? - Stack Overflow
I'm trying to extract numbers from a string representing coordinates (43ยฐ20'30"N) but some of them end in a decimal number (43ยฐ20'30.025"N) trying to figure out a way to pull out all numbers between any non-number but also recognizing that 30.025 is a number. 2023-05-24T12:25:52.657Z+00:00 ... This won't detect 0 if it's the first ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I extract the number at the beginning of a string in Python 3.7? - Stack Overflow
I'm using Python 3.7. I'm having difficulty extractng a number from teh beginning of a string. More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 5, 2019
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-extract-numbers-from-string
Python | Extract Numbers from String - GeeksforGeeks
November 10, 2025 - It checks each word with isdigit() and directly collects all the numeric ones in a single line. It can only extract positive numbers. ... isdigit() method checks if a character is a number.
๐ŸŒ
Tutorial Reference
tutorialreference.com โ€บ python โ€บ examples โ€บ faq โ€บ python-how-to-extract-first-digit-of-number
How to Extract Digits from Numbers and Strings in Python | Tutorial Reference
March 29, 2025 - re.search(r'\d+', my_str): Searches for the first occurrence of one or more digits (\d+) in the string. r'\d+': The regular expression. \d matches any digit (0-9), and + means "one or more". if match:: Checks if a match was found. re.search returns a match object if successful, and None otherwise. ...
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 264570 โ€บ extract-first-digit-in-a-number
python - Extract first digit in a number [SOLVED] | DaniWeb
Use atof() for float points. import locale nmbr = raw_input("enter a number") firstNum = nmbr[0] realNumber = locale.atoi(firstNum) โ€” Tech B 48 Jump to Post ... if digit is in string form: digit[:1] if the digit is an integer: str(digit)[:1] ...
๐ŸŒ
Know Program
knowprogram.com โ€บ home โ€บ how to get the first digit of a number in python
How to Get the First Digit of a Number in Python - Know Program
August 22, 2022 - We need to pass the index position ... starts from 0, So to get the first character of the given string pass the index position 0 in the [ ] operator....
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python extract numbers from string
Python Extract Numbers From String - Spark By {Examples}
May 21, 2024 - You can extract numbers from a string using list comprehension and the isdigit() method. For instance, you can start with an input string string that contains a mix of letters, numbers, and punctuation.
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74251965 โ€บ extract-the-first-number-from-a-string-number-range
python - Extract the first number from a string number range - Stack Overflow
October 30, 2022 - data = {'price': ['0','100-200','200-300']} df = pd.DataFrame(data) df['price'] = df.price.str.extract(r'(\d+)-?').astype(int) # same result ... Sign up to request clarification or add additional context in comments. ... This will only store first number from the range.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-get-first-digit-of-number
Get the first or first N digits of a Number in Python | bobbyhadz
April 10, 2024 - The syntax for string slicing is my_str[start:stop:step]. The start index is inclusive, whereas the stop index is exclusive (up to, but not including). The slice my_str[:2] starts at index 0 and goes up to, but not including index 2. Alternatively, ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-find-first-number-in-a-string-using-regex
How to find first number in a string using regex - Quora
Answer (1 of 5): You can use [code ](\d+) [/code]and this will match and whatever language you use, it probably has methods to get all of matches (as an array, list etc..), and you can get the first one only. Unfortunately, I donโ€™t know to get only the first match in regex. Demo EDIT: As Mana...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-extract-string-till-numeric
Python โ€“ Extract String till Numeric | GeeksforGeeks
April 22, 2023 - The next() function returns the next item in an iterator, which in this case is the index of the first numeric character. If no numeric character is found, len(test_str) is returned instead.
Top answer
1 of 4
3

As the comments on that answer note, in Python 3, filter returns a filter generator object, so you must iterate over it and build a new string before you can call int:

>>> s = '3 reviews'
>>> filter(str.isdigit, s)
<filter object at 0x800ad5f98>
>>> int(''.join(filter(str.isdigit, s)))
3

However, as other answers in that same thread point out, this is not necessarily a good way to do the job at all:

>>> s = '3 reviews in 12 hours'
>>> int(''.join(filter(str.isdigit, s)))
312

It might be better to use a regular expression matcher to find the number at the front of the string. You can then decide whether to allow signs (+ and -) and leading white-space:

>>> import re
>>> m = re.match(r'\s*([-+])?\d+', s)
>>> m
<_sre.SRE_Match object; span=(0, 1), match='3'>
>>> m.group()
'3'
>>> int(m.group())
3

Now if your string contains a malformed number, m will be None, and if it contains a sign, the sign is allowed:

>>> m = re.match(r'\s*([-+])?\d+', 'not a number')
>>> print(m)
None
>>> m = re.match(r'\s*([-+])?\d+', '  -42')
>>> m
<_sre.SRE_Match object; span=(0, 5), match='  -42'>
>>> int(m.group())
-42

If you wish to inspect what came after the number, if anything, add more to the regular expression (including some parentheses for grouping) and use m.group(1) to get the matched number. Replace \d+ with \d* to allow an empty number-match, if that's meaningful (but then be mindful of matching a lone - or + sign, if you still allow signs).

2 of 4
0

You can amend the top answer in the link you send to this:

str1 = "3158 is a great number"
print(int("".join(filter(str.isdigit, str1))))
#3158

As to why the answer doesn't work now, I'm not sure.

๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ string โ€บ extract-digits-from-python-string
2 Easy Ways to Extract Digits from a Python String - AskPython
May 5, 2026 - inp_str = "Order-1034-spent-500" number = int("".join(char for char in inp_str if char.isdigit())) print(number) print(type(number)) ... One thing to watch: isdigit() returns True for Unicode digit characters like the fullwidth digit โ€œ๏ผ’โ€ (U+FF12) in addition to plain ASCII digits. If you are processing user input or data from external sources, this may or may not be what you want. For ASCII-only digits, you can check '0' <= char <= '9' instead. Pythonโ€™s regular expression module handles digit extraction with the pattern \d+, which matches one or more consecutive digit characters.
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-find-number-in-string
How to Find Numbers in a String Using Python
November 4, 2025 - From the output, you can see that the filter() method with isdigit() and join() method extracts the number from the string.