Regex for Decimal Number
Regex Pattern
^-?\d+(?:\.\d+)?$Optionally signed decimal number
Quick Answer
The regex pattern for decimal number is `^-?\d+(?:\.\d+)?$`. Optionally signed decimal number. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| 0 | ✓ Matches |
| 42 | ✓ Matches |
| -3.14 | ✓ Matches |
| 1.5 | ✓ Matches |
| -100 | ✓ Matches |
| 1.5.2 | ✗ No match |
| .5 | ✗ No match |
| 1,5 | ✗ No match |
| abc | ✗ No match |
Code Examples
javascript
const regex = /^-?\d+(?:\.\d+)?$/; const isValid = regex.test(value);
python
import re
pattern = r'^-?\d+(?:\.\d+)?$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^-?\d+(?:\.\d+)?$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^-?\d+(?:\.\d+)?$/', $value)) {
echo "valid";
}java
String pattern = "^-?\\d+(?:\\.\\d+)?$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
Positive Integer
Integers greater than zero (no leading zeros)
Non-negative Integer
Zero or positive integer
Hex Color Code
3 or 6-digit hex color with optional #
USD Currency
US dollar amount with optional $ and thousands separators
Email Address
Validates a standard email address format
Email (RFC 5322 Compliant)
RFC 5322 compliant email validation with label length limits