Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to write a Python regular expression to get numbers except decimal?
Python's regular expressions (regex) allow us to search, extract, and manipulate patterns in strings. When we want to extract only whole numbers from a string (excluding decimals), we can use re.findall() method with a specific regex pattern.
The regex pattern \b\d+\b is designed to match integer patterns specifically. This pattern matches numbers like 10, 245, 89 but not 12.34 or 0.56 (decimals).
- \b ? Word boundary to ensure we match standalone numbers
- \d+ ? One or more digits (0?9)
- \b ? Ending word boundary
Basic Extraction of Whole Numbers
This approach uses the simple regex pattern \b\d+\b to extract numbers except decimals. The re.findall() function searches through text to find all matches of the given pattern and returns them as a list ?
import re text = "The temperatures are 30, 25.5, and 40 today." pattern = r'\b\d+\b' matches = re.findall(pattern, text) print(matches)
['30', '25', '5', '40']
Note: The decimal 25.5 is split into '25' and '5' because the word boundary treats the dot as a separator.
Extracting Numbers from Mixed Content
When working with strings containing letters, symbols, and mixed numerical formats, the same regex pattern filters out only the integers ?
import re text = "There are 5 apples, 10.5 oranges, and 15 bananas!" pattern = r'\b\d+\b' matches = re.findall(pattern, text) print(matches)
['5', '10', '5', '15']
The pattern successfully extracts '5' and '15' as whole numbers, while splitting the decimal '10.5' into '10' and '5'.
Handling Negative Numbers
To capture negative whole numbers, we enhance the pattern to \b-?\d+\b, where the -? makes the negative sign optional ?
import re text = "The temperatures are -10, -5, 0, and 20 degrees." pattern = r'\b-?\d+\b' matches = re.findall(pattern, text) print(matches)
['-10', '-5', '0', '20']
Alternative Pattern for Better Decimal Handling
If you want to completely avoid matching parts of decimal numbers, use a negative lookahead pattern ?
import re text = "Numbers: 30, 25.5, 40, 3.14159" pattern = r'\b\d+(?!\.\d)\b' matches = re.findall(pattern, text) print(matches)
['30', '40']
This pattern (?!\.\d) uses negative lookahead to ensure the number is not followed by a decimal point and digits.
Comparison of Patterns
| Pattern | Matches | Use Case |
|---|---|---|
\b\d+\b |
All digit sequences | Simple extraction, splits decimals |
\b-?\d+\b |
Includes negative numbers | When negative integers needed |
\b\d+(?!\.\d)\b |
Only standalone integers | Avoid decimal number parts |
Conclusion
Use \b\d+\b for basic integer extraction from text. For negative numbers, add the optional minus sign with \b-?\d+\b. When you need to completely avoid decimal parts, use negative lookahead patterns for more precise matching.
